Jump to content


Photo

mysql_query()!!


  • Please log in to reply
4 replies to this topic

#1 jumbo

jumbo
  • Members
  • PipPip
  • Member
  • 13 posts

Posted 29 March 2006 - 12:04 AM

hi there

i 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:

<?
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>




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

#2 shortj75

shortj75
  • Members
  • PipPipPip
  • Advanced Member
  • 118 posts
  • LocationPort Huron,mi

Posted 29 March 2006 - 12:23 AM

try this
<?
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>

this should work
The Short One

hopfully this helps if it doesn't sorry
i won't write the code for you!!! you don't learn that way

#3 norman100

norman100
  • Members
  • PipPip
  • Member
  • 24 posts

Posted 29 March 2006 - 01:01 AM

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.



#4 jumbo

jumbo
  • Members
  • PipPip
  • Member
  • 13 posts

Posted 30 March 2006 - 01:17 AM

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?

#5 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 30 March 2006 - 02:55 AM

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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users