954,585 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

What value is returned from an empty sql query?

Sorry been out of commision for a few months.
Got a newbe question.

What value is returned from an sql query that returns no rows?

Example:

$a = "SELECT * FROM a WHERE b = c";
$b = my_sql_query($a);


Note:
Table Name - a
Table Field - b
Table Field Value - c

Question:
In the table there is no value of c and hence nothing is returned
What is the value of $b?

Thanks, Regards X

PS: I am assuming 0 or NULL but nothing seems to be working, thanks.

OmniX
Practically a Master Poster
656 posts since Dec 2007
Reputation Points: 31
Solved Threads: 10
 

Then if you do mysql_num_rows($b) it will return 0 and if you attempt to fetch an array from the query then mysql will throw an error. So the value of b is still the query execute command but it just won't execute and instead will throw an error.

cwarn23
Occupation: Genius
Team Colleague
3,033 posts since Sep 2007
Reputation Points: 413
Solved Threads: 259
 
In the table there is no value of c and hence nothing is returned What is the value of $b?


$b will still hold a result resource. CheckReturn values here..
http://in.php.net/function.mysql-query

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

I dont think that is correct because when I make the variable that holds the result == 0 / NULL it dosent work.

How could you demonstrate that:

$a = "SELECT * FROM a WHERE b = c";
$b = mysql_query($a);
if($b == 0) {
 echo "No rows retrieved";
} else {
 echo "Rows retrieved";
}


So anything something like this possible?

THanks, Regards X

OmniX
Practically a Master Poster
656 posts since Dec 2007
Reputation Points: 31
Solved Threads: 10
 

You have to get the number of rows.

$a = "SELECT * FROM a WHERE b = c";
$b = mysql_query($a);
$num_rows = mysql_num_rows($b);
if($num_rows == 0) {
 echo "No rows retrieved";
} else {
 echo "Rows retrieved";
}

Is that what you were asking?

buddylee17
Practically a Master Poster
697 posts since Nov 2007
Reputation Points: 232
Solved Threads: 137
 

Your skipping a step :P
Because you cant preform num_rows if $b returns no rows and throws an error.
Been trying to come up with a solution but it is annoying me - hopefully I figure it out soon :)

OmniX
Practically a Master Poster
656 posts since Dec 2007
Reputation Points: 31
Solved Threads: 10
 

Actually, I modified your query to test the code in my db prior to posting and it outputted "No rows retrieved'". Also, when I run the code like so:

$sql = 'SELECT * FROM `absent faculty table` WHERE absentid = "blah"'; 
$result=mysql_query($sql,$conn);
$num_rows = mysql_num_rows($result);
echo $num_rows;

it returns 0 every time.

Have you got an error in your query?

buddylee17
Practically a Master Poster
697 posts since Nov 2007
Reputation Points: 232
Solved Threads: 137
 

You got it all wrong. $b doesn't return any rows. It just return result resource, which can be used in
* mysql_num_rows to know how many rows were returned.
* mysql_fetch_array/mysql_fetch_assoc/mysql_fetch_row/mysql_fetch_object to get the values of the rows .

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

nav33n is correct it is returning a result resouce (but I was meaning to say if that result resource returns no rows).

buddylee17 that returned 0 always? Intresting... My query works because when it does "retrieve a row" there is no error but when "dosent retrieve a row" there is an error :(

Anyways to check if the result_resource contains "no rows" so then dosent throw an error?

Hmmm... Actually I think I know what may be the problem I think because I am calling the mysql_num_rows from a function then directly?

Ill bbs, after I have tinkered. Thanks for the input. :)

Ya solved it in .5 seconds with your help, ha (the function was causing the errors).

Thanks Guys

function rows($a)	{
 $b = mysql_num_rows($a) or die("Error: ");
 return $b;
}


using 'rows' instead 'mysql_num_rows' throws the error.
Anyone can help me fix my code? Thanks

OmniX
Practically a Master Poster
656 posts since Dec 2007
Reputation Points: 31
Solved Threads: 10
 
Hmmm... Actually I think I know what may be the problem I think because I am calling the mysql_num_rows from a function then directly?


May be. Are you passing this result resource to the function ? If you post relevant code, it would be very helpful !

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

Im on dial up nav, your beating me before I can even fix my posts! haha

OmniX
Practically a Master Poster
656 posts since Dec 2007
Reputation Points: 31
Solved Threads: 10
 

:D So, its fixed now ?

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

the post, not the function :(

Im just resorting to use the straight mysql but I prefer to call my function, so any ideas how I can fix it?

OmniX
Practically a Master Poster
656 posts since Dec 2007
Reputation Points: 31
Solved Threads: 10
 

The function is fine. The only place where you can go wrong is while passing the argument to the function. Make sure $a is a valid result resource.

function getRows($result_resource) {
 return mysql_num_rows($result_resource);
}
$query = "select * from table where column='somevalue'";
$result = mysql_query($query);
$totalRows = getRows($result);
echo $totalRows;

Oh, btw, If your query isn't a valid one, ie., if the table or the column doesn't exist, it will return an error ! :)

Cheers!
Nav

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

Ya but nav my query is fine it just the result resource returns no "rows" and hence throws an error when it goes through that function. Dont forget I have "or die" statement connected to catch and throw errors. Thats what is prolly getting me into trouble? (but it shouldnt because the result resouce is not an error just contains no rows?)... lol im confused now *headache* :(

OmniX
Practically a Master Poster
656 posts since Dec 2007
Reputation Points: 31
Solved Threads: 10
 

What exactly is the error message :-/

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 
Your skipping a step :P Because you cant preform num_rows if $b returns no rows and throws an error. Been trying to come up with a solution but it is annoying me - hopefully I figure it out soon :)


Hi, I have just done a few tests to see what the mysql_query() function really returns on the technical side and it seems all it returns is instructions on how to access the data within the mysql database. Just thought I would let you's all know. And that I believe is what classifies it as a recourse rather than a result.

cwarn23
Occupation: Genius
Team Colleague
3,033 posts since Sep 2007
Reputation Points: 413
Solved Threads: 259
 

This is the logic "I think" it is doing.

Because the result resouce contains 'no rows' and then goes to the function to preform the mysql_num_rows it cannot(due to having no rows - I dont know why) and then goes to the or die statement and throws the "Error".

I think if you try the below code it will justify the above logic:

function getRows($result_resource) {
 $row = mysql_num_rows($result_resource) or die("Error");
 return $row;
}
$query = "select * from table where column='somevalue'";
$result = mysql_query($query);
$totalRows = getRows($result);
echo $totalRows;


So any ideas on a solution (minus not using the function :D)?

THanks

OmniX
Practically a Master Poster
656 posts since Dec 2007
Reputation Points: 31
Solved Threads: 10
 
Because the result resouce contains 'no rows' and then goes to the function to preform the mysql_num_rows it cannot(due to having no rows - I dont know why) and then goes to the or die statement and throws the "Error".


No. result resource simply wouldn't know if it returns "No rows" or "Rows". Does the above function work for you ? If it doesn't, then, I am sorry, I don't know what else to say.

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

Try the proper error reporting and is as follows:

function getRows($result_resource) {
 $row = mysql_num_rows($result_resource) or die(mysql_error());
 return $row;
}
$query = "select * from table where column='somevalue'";
$result = mysql_query($query);
$totalRows = getRows($result);
echo $totalRows;

Post what error that throws and will give you better info on how to solve it.

cwarn23
Occupation: Genius
Team Colleague
3,033 posts since Sep 2007
Reputation Points: 413
Solved Threads: 259
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You