Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Not Available

Guest
Original poster
Jun 30, 2009
318
0
Well... I've come across a problem I can't seem to be able to find a fix for. I've been struggling for an hour now.

I've got this database, and a web form. The web form allows me to enter a unique code somebody has been provided with, and, when I submit the form, the user has to be erased from the database. This part works, but because I can't keep track of the codes in the database, I've written a test condition so that,

IF the code is not found in the database
THEN echo an error message
IF the code is found in the database
THEN go ahead and remove the user

The problem is that it does not test correctly. Whether I don't enter anything, or the code does not exist, I get the confirmation message instead of the error one. I've tried with if($result), isset and empty, but none of them worked. Here is my code:

Code:
if(!empty($result)) {
        // Do something
}
if(empty($result)) {
	// Do something
}
 
This isn't the proper use of the empty function. The isset function won't due to the variable still have a value, just no results assigned to it.

Try using mysql_num_rows,

PHP:
// If found
if (mysql_num_rows($result) > 0) {
 ...
}
Or, if you're using mysqli, you can use the num_rows property.
 
Yeahp, this is the right way to do it. Thanks for the tip!

I'd shorten it a bit and write if(mysqli_num_rows($result)), as 0 is equal to false, so if there are more than 0 rows, the condition is true.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.