Jump to content

[SOLVED] Hwo to check if SQL query has returned a result


williamZanelli

Recommended Posts

Hey guys,

 

I'm trying to extract some data from a database, What I want is, if there's no result returned, to redirect to some page, otherwise redirect to another page.

 

My code is something like

 

$sql = "Select * FROM quiz WHERE userID = $id";
$result = mysql_query ($sql);

if ($result){

//redirect to  nodata page
}
else{

//do something else

 

The "if ($result)" statment doesnt seem to work, as it is true, even when the query returns no results.

 

Any ideas on how I can fix this??

 

Thanks in advance for your thoughts.

 

 

Link to comment
Share on other sites

DarkWater,

 

Thanks for the response.

 

The code provided yourself solves the stated problem.

 

What is the prupose of sanitize?

 

I do have the "die" code, but only in one place, when I connect to the database

 

$con = mysql_connect($servername,$username,$password);
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

 

I'm not sure how to incorprate this into my code.

 

Thanks for your help.

 

William

Link to comment
Share on other sites

mysql_query will return a result source on a select query if results are returned, and FALSE if nothing is returned.  If nothing is returned, your condition should fail.  Your code block is written correctly, which means the problem is not in the condition, but somewhere else.  Perhaps you can start with doing some kind of var dump on $result inside the condition (comment out the redirect, of course), and taking a look at what's being returned.

Link to comment
Share on other sites

mysql_query will return a result source on a select query if results are returned, and FALSE if nothing is returned.  If nothing is returned, your condition should fail.  Your code block is written correctly, which means the problem is not in the condition, but somewhere else.  Perhaps you can start with doing some kind of var dump on $result inside the condition (comment out the redirect, of course), and taking a look at what's being returned.

 

I'm fairly sure it only returns FALSE when there was an actual error with the query, otherwise you'd always have your "or die()"'s activating if there were no results.

Link to comment
Share on other sites

Only a query that fails to execute (no connection to a database server, no database selected, syntax error) returns a FALSE value. A query that executes but has zero rows in the result set is a successful query and returns a result resource.

 

Yeah, so it still populates the variable even though it's just a blank result set.  You need to use mysql_num_rows() here.

Link to comment
Share on other sites

Ah you guys are right, I guess I had a foobar moment.  I checked the manual and there it was, just like you said.  Thing is, I already knew that...don't know what caused me to think that right now...maybe it's cuz I haven't gone to bed yet...

 

I think that Foobar should be a candy bar for programmers.  Honestly.  I think that a good portion of programmers have used classes or functions named "foo", "bar", or "foobar" at least twice.

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.