I am trying to display the number of rows I have in my database, but instead of the number being displayed on my page I just get the word 'array'

My code is below

$productCountSql = "SELECT COUNT(*) FROM products WHERE `user_id` = '" . $_SESSION['user_id'] . "'";
		$productCountResult = mysql_query($productCountSql);
		$countProducts = mysql_fetch_assoc($productCountResult);
		echo "<tr><td>Products uploaded:</td><td>$countProducts products</td></tr>";

If anyone can point me in the correct direction that would be great. Thankyou

Recommended Answers

All 2 Replies

The result sets are always an array. Even if it contains one column.

A couple minor changes are needed:

$productCountSql = "SELECT COUNT(*) as rowcount FROM products WHERE `user_id` = '" . $_SESSION['user_id'] . "'";
$productCountResult = mysql_query($productCountSql);
$countProducts = mysql_fetch_assoc($productCountResult);
echo "<tr><td>Products uploaded:</td><td>{$countProducts['rowcount']} products</td></tr>";

In the sql query I assign the count result an alias. This alias becomes the column name and lets you easily get at the result in PHP. Otherwise it shows up as index 0 or something hard to read later.

Then in the echo I reference the array with the index name I created in the query. Because this is an array with an index within quotes, the expression is wrapped in {curly braces}.

ok thankyou! that worked perfectly
Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.