Jump to content

mysql_query()!!


jumbo

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
Link to comment
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
Link to comment
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.

Link to comment
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.
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.