abyssal Posted October 18, 2011 Share Posted October 18, 2011 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 ? Quote Link to comment https://forums.phpfreaks.com/topic/249320-mysql_fetch_row-error/ Share on other sites More sharing options...
Psycho Posted October 18, 2011 Share Posted October 18, 2011 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); Quote Link to comment https://forums.phpfreaks.com/topic/249320-mysql_fetch_row-error/#findComment-1280216 Share on other sites More sharing options...
abyssal Posted October 18, 2011 Author Share Posted October 18, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/249320-mysql_fetch_row-error/#findComment-1280332 Share on other sites More sharing options...
floridaflatlander Posted October 18, 2011 Share Posted October 18, 2011 Did you do this? $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); I write mine $r = mysqli_query($dbc, $q) or die("Error: ".mysqli_error($dbc)); Quote Link to comment https://forums.phpfreaks.com/topic/249320-mysql_fetch_row-error/#findComment-1280339 Share on other sites More sharing options...
Psycho Posted October 18, 2011 Share Posted October 18, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/249320-mysql_fetch_row-error/#findComment-1280355 Share on other sites More sharing options...
abyssal Posted October 20, 2011 Author Share Posted October 20, 2011 Thanks ! I made your changes, got the point now. Quote Link to comment https://forums.phpfreaks.com/topic/249320-mysql_fetch_row-error/#findComment-1280920 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.