Welcome to WebmasterWorld Guest from 54.196.208.6

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Deleting nothing from a database!

a question..

     
10:23 am on Sep 28, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member dreamcatcher is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 30, 2003
posts:3719
votes: 0


I have this script I`m working on that displays an unsubscribe link in the e-mails that get sent out. So, its something like:

"blahblah.php?action=unsubscribe&name=$name&email=$email"

Now when its clicked on it deletes the e-mail from the database.

if ($action == "unsubscribe")

{

$query = "DELETE FROM list WHERE listemail = '$email'";
$result = mysql_query($query);

if (!$result)

{

echo "Account has already been removed!";

}

emailRemoved();

}

Now the problem I have is that if the link is clicked on a second time, when there is no e-mail address in the database, it still displays the emailRemoved() function instead of the echo statement.

I`ve tried (!$result) and (empty($result)) but its still the same.

So how do I get the echo statement to display if theres nothing to delete?

Thanks!

10:41 am on Sept 28, 2003 (gmt 0)

New User

10+ Year Member

joined:Feb 23, 2003
posts:27
votes: 0


Hi,

The problem is that a DELETE query always returns TRUE, as long as the query was formally OK. From the PHP manual:

A non-FALSE return value
means that the query was legal and could be executed by
the server. It does not indicate anything about the number of
rows affected or returned. It is perfectly possible for a query
to succeed but affect no rows or return no rows.

What you need is
[url=http://www.php.net/manual/en/function.mysql-affected-rows.php]mysql_affected_rows()[/url]
, After a DELETE query it returns the number of deleted rows, and obviously 0 if no row has been deleted.
This should do the trick.
11:16 am on Sept 28, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member dreamcatcher is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 30, 2003
posts:3719
votes: 0


Thanks mapostel, it works like a charm!

:)