What value is returned from an empty sql query?

Thread Solved

Join Date: Dec 2007
Posts: 624
Reputation: OmniX is an unknown quantity at this point 
Solved Threads: 10
OmniX's Avatar
OmniX OmniX is online now Online
Practically a Master Poster

What value is returned from an empty sql query?

 
0
  #1
Feb 26th, 2009
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:
  1. $a = "SELECT * FROM a WHERE b = c";
  2. $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.
"You never stop learning." - OmniX
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,538
Reputation: cwarn23 has a spectacular aura about cwarn23 has a spectacular aura about cwarn23 has a spectacular aura about 
Solved Threads: 137
cwarn23's Avatar
cwarn23 cwarn23 is offline Offline
Posting Virtuoso

Re: What value is returned from an empty sql query?

 
0
  #2
Feb 26th, 2009
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.
Try not to bump 10 year old threads as it can be really annoying.
http://syntax.cwarn23.net/
Smilies: ^_* +_+ v_v -_- *~*`
My favourite PC. - Oopy Doopy Do 2U2!
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,761
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 332
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: What value is returned from an empty sql query?

 
0
  #3
Feb 26th, 2009
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. Check Return values here..
http://in.php.net/function.mysql-query
Last edited by nav33n; Feb 26th, 2009 at 4:52 am.
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 624
Reputation: OmniX is an unknown quantity at this point 
Solved Threads: 10
OmniX's Avatar
OmniX OmniX is online now Online
Practically a Master Poster

Re: What value is returned from an empty sql query?

 
0
  #4
Feb 26th, 2009
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:
  1. $a = "SELECT * FROM a WHERE b = c";
  2. $b = mysql_query($a);
  3. if($b == 0) {
  4. echo "No rows retrieved";
  5. } else {
  6. echo "Rows retrieved";
  7. }

So anything something like this possible?

THanks, Regards X
"You never stop learning." - OmniX
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 596
Reputation: buddylee17 has a spectacular aura about buddylee17 has a spectacular aura about 
Solved Threads: 125
buddylee17's Avatar
buddylee17 buddylee17 is offline Offline
Posting Pro

Re: What value is returned from an empty sql query?

 
0
  #5
Feb 26th, 2009
You have to get the number of rows.
  1. $a = "SELECT * FROM a WHERE b = c";
  2. $b = mysql_query($a);
  3. $num_rows = mysql_num_rows($b);
  4. if($num_rows == 0) {
  5. echo "No rows retrieved";
  6. } else {
  7. echo "Rows retrieved";
  8. }
Is that what you were asking?
Lost time is never found again.
- Benjamin Franklin
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 624
Reputation: OmniX is an unknown quantity at this point 
Solved Threads: 10
OmniX's Avatar
OmniX OmniX is online now Online
Practically a Master Poster

Re: What value is returned from an empty sql query?

 
0
  #6
Feb 26th, 2009
Your skipping a step
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
Last edited by OmniX; Feb 26th, 2009 at 11:42 pm.
"You never stop learning." - OmniX
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 596
Reputation: buddylee17 has a spectacular aura about buddylee17 has a spectacular aura about 
Solved Threads: 125
buddylee17's Avatar
buddylee17 buddylee17 is offline Offline
Posting Pro

Re: What value is returned from an empty sql query?

 
1
  #7
Feb 26th, 2009
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:
  1. $sql = 'SELECT * FROM `absent faculty table` WHERE absentid = "blah"';
  2. $result=mysql_query($sql,$conn);
  3. $num_rows = mysql_num_rows($result);
  4. echo $num_rows;
it returns 0 every time.

Have you got an error in your query?
Lost time is never found again.
- Benjamin Franklin
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,761
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 332
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: What value is returned from an empty sql query?

 
0
  #8
Feb 26th, 2009
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 .
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 624
Reputation: OmniX is an unknown quantity at this point 
Solved Threads: 10
OmniX's Avatar
OmniX OmniX is online now Online
Practically a Master Poster

Re: What value is returned from an empty sql query?

 
0
  #9
Feb 26th, 2009
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

  1. function rows($a) {
  2. $b = mysql_num_rows($a) or die("Error: ");
  3. return $b;
  4. }

using 'rows' instead 'mysql_num_rows' throws the error.
Anyone can help me fix my code? Thanks
Last edited by OmniX; Feb 27th, 2009 at 12:00 am.
"You never stop learning." - OmniX
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,761
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 332
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: What value is returned from an empty sql query?

 
0
  #10
Feb 26th, 2009
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 !
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 2224 | Replies: 26
Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC