jumbo Posted March 29, 2006 Share Posted March 29, 2006 hi therei know that mysql_query() returns TRUE when it succeeds and FALSE if not...but it doesnt work well for me...i have a table where i got emails... and im trying to design a simple code that allows me to check whether an email is there or not... the code looks like this:[code]<?mysql_connect('localhost', 'me_user', 'password');mysql_select_db('me_database');if (isset($_POST['submit'])) {$em1=$_POST['em'];if( mysql_query("SELECT `email` FROM `emails` where ('email' = '$em1')")) //table is called emails, and column is called email{echo "The email is already in the DB";}else {echo "This email address is not in the DB";}?><html><body><form method="post"> <input type="text" name="em" size="20"><input type="submit" name="submit" value="submit"></body></html>[/code]when i enter any email, whether it is there or not, it always return The email is already in the DB!!!What is wrong with this code? or is it the mysql_query()??Thanx Quote Link to comment https://forums.phpfreaks.com/topic/6056-mysql_query/ Share on other sites More sharing options...
shortj75 Posted March 29, 2006 Share Posted March 29, 2006 try this [code]<?mysql_connect('localhost', 'me_user', 'password');mysql_select_db('me_database');if (isset($_POST['submit'])) {$em1=$_POST['em'];$checkemadd=mysql_query("SELECT `email` FROM `emails` where email='$em1'"); $checkemadd2=mysql_num_rows($checkemadd); if($checkemadd2 > 0){echo "The email is already in the DB";}else {echo "This email address is not in the DB";} }?><html><body><form method="post"> <input type="text" name="em" size="20"><input type="submit" name="submit" value="submit"></body></html>[/code]this should work Quote Link to comment https://forums.phpfreaks.com/topic/6056-mysql_query/#findComment-21777 Share on other sites More sharing options...
norman100 Posted March 29, 2006 Share Posted March 29, 2006 Thats actually a common misconception, mysql_query will return true if the query ran, it will also return true if the query ran but didnt return any results.The feature which u require is mysql_num_rows, this returns the amount of rows as an integer that a query returns. Quote Link to comment https://forums.phpfreaks.com/topic/6056-mysql_query/#findComment-21791 Share on other sites More sharing options...
jumbo Posted March 30, 2006 Author Share Posted March 30, 2006 Great!It worked, thanx!Yeah, that turned out to be a misconception...So mysql_query() returns false if the query doesnt succeed because of the table missing, or the filed missing for example right? Quote Link to comment https://forums.phpfreaks.com/topic/6056-mysql_query/#findComment-22149 Share on other sites More sharing options...
akitchin Posted March 30, 2006 Share Posted March 30, 2006 mysql_query() will return FALSE if it has failed, period. it can fail for any number of reasons over and above missing table name: syntax error, misnamed references, reserved keywords, etc. otherwise it will return either a resource ID containing all the matches (which may, as others have noted, be none) or simply TRUE for those queries that do not return data.the php manual details quite well what every function returns and how to deal with your results. it should be one of your best friends. Quote Link to comment https://forums.phpfreaks.com/topic/6056-mysql_query/#findComment-22159 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.