Page 1 of 1
No results returned
Posted: Fri Jun 06, 2008 10:41 am
by fluidbyte
PHP newb here. Just curious; when I do a query, how can I determine if there are no results?
Code: Select all
$rs = mysql_query("SELECT * FROM myTable");
if (NO_RECORDS)
{
echo("Nothing found");
}
else
{
echo("Stuff found");
}
Re: No results returned
Posted: Fri Jun 06, 2008 10:46 am
by Frozenlight777
You had it pretty close. But you could do something like this.
Code: Select all
$rs = mysql_query("SELECT * FROM myTable");
if (isset($rs[0]{0})){
echo("Stuff Found");
}
else {
echo("Nothing Found");
}
or could also do something as easy as:
Code: Select all
if (isset($rs[0]{0}) OR DIE("NO RESULTS")){
}
The isset($rs[0]{0} part checks to see if there was something set into the array from the query... so what it does is checks to see if the query has a result greater than 0, if there's something in the result then it would obviously be greater than 0....
I hope that helps and I'm sure there's probably a better way of doing it. That's just what I'm used to.
Re: No results returned
Posted: Fri Jun 06, 2008 12:09 pm
by dbemowsk
This should do...
Code: Select all
$rs = mysql_query("SELECT * FROM myTable");
if ($rs) { //if the query returned true then the query was OK
if (mysql_num_rows($rs) > 0) { //check how many results were returned
[your code]
} else {
echo "No results returned";
}
} else {
echo mysql_error();
}
Re: No results returned
Posted: Fri Jun 06, 2008 12:29 pm
by fluidbyte
Ok, that works, now I'm having problems with an increment.
Basically it's looping through in order by navPosition, and I want it to set the value to 1,2,3,4... but every time it loops through it's just putting "1" in for every navPosition. Any ideas?
Code: Select all
if (!isset($rs[0]{0}))
{
$counter = 1;
while ($row = mysql_fetch_array($rs))
{
mysql_query("UPDATE " . $System["tblPrefix"] . "_navigation SET navPosition=" . $counter . " WHERE navID=" . $row["navID"]);
$counter++;
}
}
}
Re: No results returned
Posted: Fri Jun 06, 2008 1:17 pm
by dbemowsk
It is generally not good practice to use double quotes in an associative array. Single quotes should be used. Try changing your $System["tblPrefix"] and $row["navID"] to $System['tblPrefix'] and$row['navID']. You are using double quotes for your query string, so you should avoid using them in your array reference anyway. Other than that I cannot see a problem in your code.
Re: No results returned
Posted: Fri Jun 06, 2008 1:28 pm
by fluidbyte
As a newb, and a very curious one, why is it bad to use double quotes in an associative array?
Re: No results returned
Posted: Fri Jun 06, 2008 3:02 pm
by dbemowsk
Consider this example. Many coders like to use their variables inside the quotes like this:
Code: Select all
$first = "The first string in the array";
$second = "The second string in the array";
echo "This is $first, and this is $second";
Using variables inside quotes is perfectly legal with PHP. However, if we change that code a bit like this:
Code: Select all
$arr["first"] = "The first string in the array"; //defining your arrays like this will parse
$arr["second"] = "The second string in the array"; //just fine using the double quotes
echo "This is $arr["first"], and this is $arr["second"]"; //This however will throw an error
The reason is that your string is getting closed after your first $arr[". It then sees the word first which it will try to take as a constant, but the error will be thrown because it thinks the string is improperly concatenated. The same holds true for the second part where the $arr["second"] is used.
For that code to work correctly, it would need to be written like this:
Code: Select all
echo "This is $arr['first'], and this is $arr['second']"; //Here the associations are correctly parsed
I am not saying that the double quotes will not work, because the code could be written like this:
Code: Select all
echo "This is " . $arr["first"] . ", and this is " . $arr["second"];
and will parse correctly.
Quotes in PHP can sometimes be a tricky thing. I just try to shy away from using double quotes when it comes to an associative array reference.
Re: No results returned
Posted: Fri Jun 06, 2008 3:08 pm
by Benjamin
Just to clear things up a bit.
mysql_query() returns a result resource on success or false if the query failed. So on a select query it can return a result resource even if there are no records.
This code is not correct:
Code: Select all
$resource = mysql_query("select * from table_name");
if ($resource)
{
}
I would recommend using the
mysql_num_rows() function.
This code is correct:
Code: Select all
$resource = mysql_query("select * from table_name");
if (mysql_num_rows($resource) > 0)
{
}
Re: No results returned
Posted: Fri Jun 06, 2008 3:29 pm
by fluidbyte
Thanks everyone, this has really been helpful.
Re: No results returned
Posted: Fri Jun 06, 2008 4:19 pm
by dbemowsk
astions, If you notice in my post above, I used both.
Code: Select all
$rs = mysql_query("SELECT * FROM myTable");
if ($rs) { //if the query returned true then the query was OK
if (mysql_num_rows($rs) > 0) { //check how many results were returned
I use the "if ($rs) {" to check if the query fails or not, then I use the mysql_num_rows function to find if there were results. The reason is that if the query fails, it will not return a result resource, rather it will equal FALSE which when supplied to mysql_num_rows($rs) will return "supplied argument is not a valid MySQL result resource".
Therefore, to say that
is an incorrect approach is only partially true. But to say that
Code: Select all
if (mysql_num_rows($rs) > 0) {
//results returned
}
is the correct approach is not completely true either. The correct approach would be to use both in conjunction with each other.
Some people use
Code: Select all
$rs = mysql_query("SELECT * FROM myTable") or die(mysql_error());
which works and is okay for code testing, but in a production environment you don't want your code just dying in the middle somewhere.