Jump to content

mysql_fetch_row error


abyssal

Recommended Posts

Hi. I have a code and I'm trying to see if an e-mail address is stored in our database.

 

$query="SELECT email FROM users WHERE email='$email'";
    $row=mysql_query($query);
    $result=mysql_fetch_row($row);
    if ($email==$result[0])
    {
        return 1;
    }
    else
    {
        return 0;
    }

 

I get this error:

Warning: mysql_fetch_row() expects parameter 1 to be resource, boolean given.

On this line: $result=mysql_fetch_row($row);

 

Anyone knows why's broken? Can I do this in a better way ?

Link to comment
Share on other sites

Your query is likely failing. You'll need to view the error to know why. Also, give your variables appropriate names to prevent confusion. In the example above you assign the "result" of the query to a variable named $row. You should use $result to hold the result of the query and $row as the variable to hold the data from a record (or row) from the result set. This is pretty much a standard. Plus, there is no reason to do a test of the value and return a 1 or 0 - the result of the comparison will do that for you. Lastly, the logic of the above does not make sense. You are pulling the email address where email address equals $email. Then you are doing a comparison between the retrieved value and $email. Except for differences in letter case they would always be the same value. If you are trying to see if that email email already exists you should be checking the count of records returned.

$query  = "SELECT COUNT(email) FROM users WHERE email='$email'";
$result = mysql_query($query) or die(mysql_error())
//This will return 1 if the email exists, 0 otherwise
return(mysql_num_rows($result);

 

Link to comment
Share on other sites

The user enters an e-mail address, stored in the variable $email. I check the database, in the users table at column named email to see if $email is found.

 

"SELECT email FROM users WHERE email='$email'"

 

Then, the function returns 1 if the email exists, and 0 else.

 

I kinda see that it fails but I couldn't think of a better solution. I'm a n00b programmer with php/sql and I'm pretty sure there are other ways to code this, but I'm trying to build this app by myself.

Link to comment
Share on other sites

I check the database, in the users table at column named email to see if $email is found.

 

"SELECT email FROM users WHERE email='$email'"

 

Then, the function returns 1 if the email exists, and 0 else.

 

That logic is flawed in the manner you implemented it.

$query="SELECT email FROM users WHERE email='$email'";
    $row=mysql_query($query);
    $result=mysql_fetch_row($row);
    if ($email==$result[0])
    {
        return 1;
    }
    else
    {
        return 0;
    }

If the email doesn't exist int he DB then $result will be the Boolean false - not an array. So, $result[0] doesn't exist either. The point is you only need to query the DB for records matching the value - then check if there were any results. You don't need to extract the results and compare them.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.