trouble with sql statement results

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
slash_gnr3k
Forum Commoner
Posts: 43
Joined: Tue Nov 28, 2006 6:41 pm

trouble with sql statement results

Post 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
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
slash_gnr3k
Forum Commoner
Posts: 43
Joined: Tue Nov 28, 2006 6:41 pm

Post by slash_gnr3k »

i got the result 0
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
slash_gnr3k
Forum Commoner
Posts: 43
Joined: Tue Nov 28, 2006 6:41 pm

Post by slash_gnr3k »

ok i understand, so howcome it isnt returning any results? it doesnt fail....
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
slash_gnr3k
Forum Commoner
Posts: 43
Joined: Tue Nov 28, 2006 6:41 pm

Post 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!
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Glad you got it sorted out.
Post Reply