2

I am not sure as to why. But the code I have created below is not working. The variable: "$num_rows" is not even being set and has no value (not even 0). Anyone know as to why this issue is occurring?

$result2 = mysql_query("SELECT * FROM `mycity_vehicles` WHERE `id` = '$vehID'");
while($row2 = mysql_fetch_array($result2))
$num_rows = mysql_num_rows($result2);
{
    if(empty($num_rows)) {
        echo "empty";
    }
    else {
        echo $num_rows;
    }
wesslayneb
  • 47
  • 1
  • 1
  • 8
  • 1
    im surprised you don't get any syntax errors –  Dec 19 '13 at 18:41
  • 1
    You have a brace in the wrong place - you need to move it from the line after `$num_rows = mysql_num_rows($result2);` to the line before. – andrewsi Dec 19 '13 at 18:41
  • @Dagon: My guess is he does, but has error reporting off. – gen_Eric Dec 19 '13 at 18:42
  • ok, wasent 100% sure if it was invalid (while being sure it was wrong) or not –  Dec 19 '13 at 18:44
  • Actually... Aside from a missing trailing brace at the end, I think it's valid syntax. – andrewsi Dec 19 '13 at 18:52
  • This was snippet of code that I pulled from a script. It was created over a year ago and I am just trying to fix some bugs that I never got around too. The syntax is ungodly horrible. Lessons learned. – wesslayneb Dec 29 '13 at 20:11

2 Answers2

2

Your syntax is off.

while($row2 = mysql_fetch_array($result2))
$num_rows = mysql_num_rows($result2);
{

should probably be

$num_rows = mysql_num_rows($result2);
while($row2 = mysql_fetch_array($result2))
{
John Conde
  • 217,595
  • 99
  • 455
  • 496
2

You did it the wrong order.

$result2 = mysql_query("SELECT * FROM `mycity_vehicles` WHERE `id` = '$vehID'");
$num_rows = mysql_num_rows($result2);
while($row2 = mysql_fetch_array($result2)) {
    if(empty($num_rows)) {
        echo "empty";
    }
    else {
        echo $num_rows;
    }
Orel Eraki
  • 11,940
  • 3
  • 28
  • 36
  • I wonder why this got downvoted. If it's because it's the same as John Conde's answer, the reason for it would (probably) be that both answers were given at approximately the same time. (Downvoter, see timestamps). – Funk Forty Niner Dec 19 '13 at 18:54
  • @Fred-ii-, I wonder the same thing.. Probably he has more "Reputation" than me so people feel the need to downvote this, altough I posted mine 6-7 seconds after he did (without knowing it). Anyways it doesn't matter to me as long as the person got the right answer. – Orel Eraki Dec 19 '13 at 19:43
  • 1
    Yes, knowing that the correct answer is given to an OP is indeed the important thing. I will upvote this, because you deserve it just as much AND that you did not deserve the downvote. Cheers ;-) – Funk Forty Niner Dec 19 '13 at 20:10