It says there are no num_rows, yet it produces them - how?

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

It says there are no num_rows, yet it produces them - how?

Post by simonmlewis »

Code: Select all

<select name='userid'>";
$resultsa = mysql_query ("SELECT id, firstname, lastname FROM admin ORDER BY firstname")or die(mysql_error());
  while ($rowa = mysql_fetch_object($resultsa)) 
  { 
  $resultsb = mysql_query ("SELECT userid, raceid FROM diary WHERE raceid = '$id' AND userid = '$rowa->id' AND pitno IS NULL")or die(mysql_error());
$num_rowsb = mysql_num_rows($resultsb);
if ($num_rowsb == 0) { $submit = no;}
if ($submit == "no")
{
while ($rowb = mysql_fetch_object($resultsb)) 
    {
    echo "<option value='$rowb->userid'>$rowa->firstname $rowa->lastname</option>";
      
    }}mysql_free_result($resultsb);
    } mysql_free_result($resultsa);
    
echo "</select>";
if ($submit == "no") { echo " No race bookings yet";}
if ($submit == NULL) { echo "<input type='submit' value='Select & Continue'>";}
echo "</form>";
I get results from $rowb (one row), but $submit is set to "no". If I put $submit next to $rowa->firstname, it puts a 'no' next to the name, by the very place the code apparently says there are no rows.

It's completely contradictory. Can anyone help tell me why $submit is getting a result at all?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: It says there are no num_rows, yet it produces them - ho

Post by AbraCadaver »

I can't read through all of that mess, but:

Code: Select all

$submit = no;
Should be:

Code: Select all

$submit = "no";
Because, no is an undefined constant.

You need to develop with:

Code: Select all

error_reporting(E_ALL);
ini_set('display_errors', '1');
Or set it in php.ini.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: It says there are no num_rows, yet it produces them - ho

Post by simonmlewis »

Makes no difference.

And I prefer to conduct myself a little more politely here.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: It says there are no num_rows, yet it produces them - ho

Post by AbraCadaver »

Once you set it to no it is always no, so you need to toggle it. By the way, I thought you were very polite in your first post:

Code: Select all

if ($num_rowsb == 0) { $submit = "no"; } else { $submit = "yes"; }
It's very hard to tell what you're trying to do, the logic seems odd, and rarely if ever would you want to do queries in a loop.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: It says there are no num_rows, yet it produces them - ho

Post by simonmlewis »

Ok maybe I ought to give you a bit more detail about what I am trying to achieve.
It needs to show all those users in the database that have made an entry for an event. But it needs to show it in First name order.

So I am getting all users first, then querying the diary to see which users have entered for that event. If there are NO users, it needs to prevent a submit button from showing, and to say "no race goers", but if there ARE users, it needs to show the button and NOT show that 'no race goers' text.

Surely, if I say

Code: Select all

if ($num_rowsb == 0) { $submit = "no"; }
And $num_rowsb is NOT 0, and there is something found (which is this scenario, there is), then $submit would be NULL, wouldn't it??
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: It says there are no num_rows, yet it produces them - ho

Post by AbraCadaver »

I can't test it but I would think that you would just do it all in one query:

[text]SELECT diary.raceid, admin.id, admin.firstname, admin.lastname
FROM diary, admin
WHERE diary.userid = admin.id AND diary.raceid = '$id' AND diary.pitno IS NULL
ORDER BY admin.firstname[/text]

Then if you have rows loop and populate the select and show the submit. If not, then display "no races" or whatever.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: It says there are no num_rows, yet it produces them - ho

Post by simonmlewis »

Thanks, will try tomorrow when am back working again. And will let you know.
I've never been any good at 'JOINS' so that's very useful.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply