Hello everybody
I'm working on MySQL
I always have to use the Count function like this:

$Q = mysql_query("Select count() from tableA",$connection);

How can I handle this query? How can I handle the number returned by this query??
Thanks.

Recommended Answers

All 6 Replies

$total = mysql_query("SELECT COUNT(*) FROM $table_and_query"); 
        $total = mysql_fetch_array($total);

You can also try this one

$link = mysql_connect("localhost", "mysql_user", "mysql_password");
mysql_select_db("database", $link);

$result = mysql_query("SELECT * FROM table1", $link);
$num_rows = mysql_num_rows($result);

echo "$num_rows Rows\n";

$total = mysql_query("SELECT COUNT(*) FROM $table_and_query");
$total = mysql_fetch_array($total);
echo "$num_rows Rows\n";

But $total is an array. So how can I handle it as an integer?

$total = mysql_query("SELECT COUNT(*) AS mycount FROM $table_and_query"); 
$total = mysql_fetch_array($total);
echo $total['mycount'];
$Q = mysql_query("Select count(*) from tableA",$connection);
$count=mysql_result($Q,0,0);

You can write the following:

$scc = mysql_query("select count(XXXX) from tableA",$connection);
$sccA = mysql_fetch_array($scc, MYSQL_NUM);
echo $sccA[0];

(Replacing XXXX with a column name).
This should make things work very well.

commented: It's a direct and concise solution +3

Thank you all.

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.