Page 1 of 1

trouble with sql statement results

Posted: Wed Jan 31, 2007 5:33 pm
by slash_gnr3k
i have the following code:

Code: Select all

$getunitid = @mysql_query("SELECT unitid FROM units WHERE name = '$newingname'");

			if(!$getunitid)
			{

				echo'<p>fail ' . mysql_error() . '</p>';

			}

			while ($row = mysql_fetch_array($getunitid))
			{

				$unitid = $row['unitid'];

				echo"test ok!";
				echo"$unitid";	
			
			}
what this code should do is run a select statement and then pass the output to a variable and display it.
The query runs fine as fail is not outputted. however the next part is the problem. test ok is not out put. therefore i think there must be something wrong with my statement but i dont know what. can anyone heklp?
thanks

Posted: Wed Jan 31, 2007 6:10 pm
by RobertGonzalez
Try running your query result through mysql_num_rows() to see if you actually have data coming out. The query could be a 0 count result which would not fire an error but still not give you anything to loop over.

Posted: Wed Jan 31, 2007 6:18 pm
by slash_gnr3k
i got the result 0

Posted: Wed Jan 31, 2007 6:20 pm
by RobertGonzalez
Then your query is returning 0 results which means the while loop will not execute and the vars inside the while loop never set/echo.

Posted: Wed Jan 31, 2007 6:25 pm
by slash_gnr3k
ok i understand, so howcome it isnt returning any results? it doesnt fail....

Posted: Wed Jan 31, 2007 6:30 pm
by RobertGonzalez
Failure and 0 results are not the same thing. Failure occurs when there is a problem with the query. The query can run successfully and still return no results. What this query is asking is:

Code: Select all

# Get all `unitid` columns from all rows in the `units` table where the column `name` is exactly equal to the value in $newingname
SELECT unitid FROM units WHERE name = '$newingname';
I would suspect the 0 result is because there are no `name` values that match whatever value is in the $newingname variable.

Posted: Wed Jan 31, 2007 6:31 pm
by slash_gnr3k
ive sorted it! u cant see what the problem was from the code ive posted. it was further up in the code! thanks a lot again for all your help it really is appreciated!

Posted: Wed Jan 31, 2007 6:36 pm
by RobertGonzalez
Glad you got it sorted out.