select count(*) query causing php to fail

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
IGGt
Forum Contributor
Posts: 173
Joined: Thu Nov 26, 2009 9:22 am

select count(*) query causing php to fail

Post 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?
internet-solution
Forum Contributor
Posts: 220
Joined: Thu May 27, 2010 6:27 am
Location: UK

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

Post 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());
Post Reply