943,918 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 8644
  • PHP RSS
You are currently viewing page 1 of this multi-page discussion thread
Feb 26th, 2009
0

What value is returned from an empty sql query?

Expand Post »
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:
php Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 31
Solved Threads: 10
Practically a Master Poster
OmniX is offline Offline
652 posts
since Dec 2007
Feb 26th, 2009
0

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

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.
Sponsor
Featured Poster
Reputation Points: 410
Solved Threads: 258
Occupation: Genius
cwarn23 is offline Offline
3,004 posts
since Sep 2007
Feb 26th, 2009
0

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

Quote ...
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.
Moderator
Featured Poster
Reputation Points: 524
Solved Threads: 356
Purple hazed!
nav33n is offline Offline
3,878 posts
since Nov 2007
Feb 26th, 2009
0

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

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:
php Syntax (Toggle Plain Text)
  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
Reputation Points: 31
Solved Threads: 10
Practically a Master Poster
OmniX is offline Offline
652 posts
since Dec 2007
Feb 26th, 2009
0

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

You have to get the number of rows.
php Syntax (Toggle Plain Text)
  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?
Reputation Points: 232
Solved Threads: 137
Practically a Master Poster
buddylee17 is offline Offline
665 posts
since Nov 2007
Feb 26th, 2009
0

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

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.
Reputation Points: 31
Solved Threads: 10
Practically a Master Poster
OmniX is offline Offline
652 posts
since Dec 2007
Feb 26th, 2009
1

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

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:
php Syntax (Toggle Plain Text)
  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?
Reputation Points: 232
Solved Threads: 137
Practically a Master Poster
buddylee17 is offline Offline
665 posts
since Nov 2007
Feb 26th, 2009
0

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

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 .
Moderator
Featured Poster
Reputation Points: 524
Solved Threads: 356
Purple hazed!
nav33n is offline Offline
3,878 posts
since Nov 2007
Feb 26th, 2009
0

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

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

php Syntax (Toggle Plain Text)
  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.
Reputation Points: 31
Solved Threads: 10
Practically a Master Poster
OmniX is offline Offline
652 posts
since Dec 2007
Feb 26th, 2009
0

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

Quote ...
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 !
Moderator
Featured Poster
Reputation Points: 524
Solved Threads: 356
Purple hazed!
nav33n is offline Offline
3,878 posts
since Nov 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: split data?
Next Thread in PHP Forum Timeline: Populate text box from database according to the selection made in listbox





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC