Page 1 of 1

select count(*) query causing php to fail

Posted: Mon Aug 09, 2010 10:57 am
by IGGt
Is there a difference if I run a MySQL Query that returns a single item rather than a whole row?

I have a query that is: SELECT COUNT(*) FROM p2 WHERE `time` BETWEEN DATE_SUB(NOW(),INTERVAL 10 MINUTE) AND NOW(); and I am trying to display it in a table, I have copied the code form another php page that I know works, but this query refuses to work.

I have:

Code: Select all

$db = "localhost:3306";
$u = "xx";
$p = "xx";
$dbs = "test";
$posCountQuery = "SELECT COUNT(*) FROM p2 WHERE `time` BETWEEN DATE_SUB(NOW(),INTERVAL 10 MINUTE) AND NOW()";

echo "<table><tr>";
	$con = mysql_connect($db,$u,$p);
	mysql_select_db($dbs, $con);
	$result = mysql_query($posCountQuery, $con);
	while ($row = mysql_fetch_assoc($result)) 	
	{
		foreach ($result as $attribute)
		print "<td>$attribute</td>";
	}
echo "</tr></table>";
The query is fine, and as far as I can tell I have copied the PHP directly from the other page, changing just the connection details and query. But I now get:

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\wamp\www\local\test2.php on line 10.

I am obviously missing something, but can't for the life of me see what it is?

Re: select count(*) query causing php to fail

Posted: Mon Aug 09, 2010 12:05 pm
by internet-solution
Your query is failing so mysql_query is returning a false (boolean) instead of a result resource. Check what the mysql server is telling you. Try

Code: Select all

$result = mysql_query($posCountQuery,  $con) or die(mysql_error());