0

I have code

$email = "[email protected]";    
$row = mysql_query("SELECT EXISTS(SELECT email FROM accounts WHERE email='".$email."');");
echo $row[0];

However, nothing is echoed.

This is strange because something is being returned because, later in the code I have a function that is CALLED:

if ( $row[0] == 0 ) { echo "Email address is available<br>";};

However: this is strange because when i put the SAME CODE into mySQL database command prompt:

enter image description here

It clearly returns 1 or TRUE.

The mysql_query is returning 0 when the same exact command in mysql command prompt returns 1. Further: I am unable to echo the result for debugging purposes.

EDIT: Please not, the regular mySQL command is returning this and ONLY this: enter image description here

EDIT: Here is there entire database: enter image description here

2
  • have you tried doing print_r($row) to see what all is being returned? Commented May 28, 2014 at 20:51
  • Yeah that just gives me a Resource id #5 Commented May 28, 2014 at 20:52

3 Answers 3

3

MySQL query gives you a ressource. After that you have to fetch the data with mysql_fetch_assoc or mysql_fetch_row or something else for example. But its better to use prepared statements with mysqli or PDO to get more security.

$email = "[email protected]";    
$res = mysql_query("SELECT EXISTS(SELECT email FROM accounts WHERE email='".myql_real_escape_string($email)."')");
$row = mysql_fetch_assoc($res);
echo $row['email'];

Answer to your question:

$email = "[email protected]";    
$res = mysql_query("SELECT email FROM accounts WHERE email='".mysql_real_escape_string($email)."')");
$numRows = mysql_num_rows($res);

if($rowRows > 0) {
    echo "Record Available";
}
Sign up to request clarification or add additional context in comments.

7 Comments

Since i'm doing SELECT EXISTS its only going to return true or false right?
Hmm if you want to check if the data exists its easier to make a count SELECT COUNT(id) as countout FROM ... WHERE ... if row['countout'] is bigger then 0 its a dataset available.
I tried $email = "[email protected]"; $res = mysql_query("SELECT EXISTS(SELECT email FROM accounts WHERE email='".$email."')"); $row = mysql_fetch_assoc($res); echo $row['email']; However i got errors: Notice: Undefined index: email in /Applications/XAMPP/xamppfiles/htdocs/TLB/create_account.php
Your second code snippet is giving me error: Notice: Undefined index: countout in
Try that with mysql_num_rows then you get the number of all counted rows.
|
0

You need to actually retrieve the result set from the query. mysql_query() just returns a resource handle for a successful select. You then need to fetch the results using mysql_fetch_* class of functions. Alternatively, you can use mysql_num_rows() to determine the number of rows returned in the result set.

In this case it is really senseless to wrap your actual query into a subquery. Just run your select and determine the number of rows:

$email = "[email protected]";    
$result = mysql_query("SELECT email FROM accounts WHERE email='".$email . "'");
if($result) {
    $row_count = mysql_num_rows($result);
    echo $row_count;
}

Also, you should not be writing new code using mysql_* functions, as these are deprecated. I would suggest mysqli or PDO extensions instead.

2 Comments

It is SELECT EXISTS though which returns true or false though correct?
@user3677286 Yes. It would. But why do you care about that sort of wrapper? You will either have a row count of 0 or > 0 in the result set. That is the same thing and removes use of subselect.
0

You need to do something like

while ($r = mysql_fetch_assoc($row)) 
{ 
    echo $r[0];                                 
}

after that code.

Let me know.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.