Problem querying database

Discussion in 'Web Design and Development' started by Not Available, Aug 6, 2009.

  1. Joined:
    Jun 30, 2009
    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:

    if(!empty($result)) {
            // Do something
    if(empty($result)) {
    	// Do something
  2. angelwatt Moderator emeritus


    Aug 16, 2005
    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,

    // If found
    if (mysql_num_rows($result) > 0) {
    Or, if you're using mysqli, you can use the num_rows property.
  3. Not Available thread starter Guest

    Jun 30, 2009
    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.

Share This Page