Jump to content

Better way to determine if a query has returned at least one row


Scaryminds

Recommended Posts

Am currently using this code, and actually thinking about it should split the query to an include in order to allow for other database drivers, on the chance we may decide to ditch the old MySQL. But I digress from the question.

 

		include($lib."dbconfig.php");

		$q = 'SELECT * FROM file WHERE this = "'.$that'";
		$result = mysql_query($q);

		if (mysql_num_rows($result) > 0) 

 

If the query doesn't pick up a row I'm getting this error yo,

 

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in D:\webpages\*\*\admin.php on line 553

 

So what would be a better way of testing if the query was successful?  :shrug:

Link to comment
Share on other sites

Your query has a syntax error in it, so the mysql_query() function is returning "false". Change the query to

<?php
$q = "SELECT * FROM file WHERE this = '$that'";
?>

and change the next line to

<?php
$result = mysql_query($q) or die("Problem with the query: $q<br>" . mysql_error());
?>

at least while you're debugging your code.

 

Ken

Link to comment
Share on other sites

Hey thanks Ken,

 

Did cut down the query as it was quite long and involved, but adding your die clause got the root issue :)

 

Unknown column 'question' in 'where clause'

 

d'uh! Been a while since I worked on the software and there are clearly some bugs we never got around to fixing  :P

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.