Page 1 of 1
It says there are no num_rows, yet it produces them - how?
Posted: Mon Jun 27, 2011 9:51 am
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?
Re: It says there are no num_rows, yet it produces them - ho
Posted: Mon Jun 27, 2011 10:53 am
by AbraCadaver
I can't read through all of that mess, but:
Should be:
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.
Re: It says there are no num_rows, yet it produces them - ho
Posted: Mon Jun 27, 2011 12:26 pm
by simonmlewis
Makes no difference.
And I prefer to conduct myself a little more politely here.
Re: It says there are no num_rows, yet it produces them - ho
Posted: Mon Jun 27, 2011 12:34 pm
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.
Re: It says there are no num_rows, yet it produces them - ho
Posted: Mon Jun 27, 2011 12:41 pm
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??
Re: It says there are no num_rows, yet it produces them - ho
Posted: Mon Jun 27, 2011 12:58 pm
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.
Re: It says there are no num_rows, yet it produces them - ho
Posted: Mon Jun 27, 2011 1:02 pm
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.