Jump to content

Archived

This topic is now archived and is closed to further replies.

jumbo

mysql_query()!!

Recommended Posts

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:

[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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

×

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.