By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,277 Members | 1,505 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,277 IT Pros & Developers. It's quick & easy.

Problem with php/mysql count() function showing

P: n/a
Hi there,
I have a really stupid and banal problem with showing the results of a
MySQL query in PHP, preciselly with MySQL count() function that gives
to a variable in PHP the result.

NOTE:
The problem here is PHP not MySQL, in MySQL everything works just
fine.

Here is the query that I wrote for getting the number of how much
records I have in my litte and tottaly trivial DB:

------------------------------ CODE ----------------------
$res = @mysql_query("SELECT COUNT(*) FROM my_data_table");

echo ($res);
------------------------------ CODE ----------------------
PS: my_data_table is a name of table not a function or other MySQL
related syntax.

The result of this query on the screen is:
Resource id #4

I was around the net and I get some info that don't give me help.
The solution from the net was to insert the value of the variable (in
this case $res) in a function like mysql_fetch_object or
mysql_fetch_row etc.

After some hours of trying to do something wtih those functions I
started to think that getting how much records I have in my DB is
almost a thing that only persons with a extremlly huge brain can
concive and only a few superior entities can made.
:)

Can some body with a huge brain help me?

Thank you in advance.
Auron

PS: I'm not a PRO I get only started to lear about PHP and MySQL.
because somebody have told me that "was" easy to make some thing with
those 2 tool.
http://eye.cc php newsgroups
Feb 21 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
auron wrote:
Hi there,
I have a really stupid and banal problem with showing the results of a
MySQL query in PHP, preciselly with MySQL count() function that gives
to a variable in PHP the result.

NOTE:
The problem here is PHP not MySQL, in MySQL everything works just
fine.

Here is the query that I wrote for getting the number of how much
records I have in my litte and tottaly trivial DB:

------------------------------ CODE ----------------------
$res = @mysql_query("SELECT COUNT(*) FROM my_data_table");

echo ($res);
------------------------------ CODE ----------------------
PS: my_data_table is a name of table not a function or other MySQL
related syntax.

The result of this query on the screen is:
Resource id #4

you are echoing the result of the connection id not the result of the query -
this is a poorly written example, but you can use it to see what went wrong (or
you can just copy/paste)
$sql = "select count(*) from my_data_table";
$link=mysql_connect($HOST,$USR,$PWD,$DB);
if( $link ) {
mysql_select_db( $DB, $link );
if( $sql ) {
// make sure there are no trailing semi-colons (ie no sql-injection)
$sql = ereg_replace( ";$", "", $sql );
$sql = ereg_replace( "\\\\", "", $sql );
$result = mysql_query( $sql, $link );
if( $result ) {
echo( "<p>Results: for '$sql;'.</p>\n" );
if( $num = mysql_num_rows( $result ) ) {
echo( "<pre>\n" );
while( $array = mysql_fetch_row( $result ) ) {
while( list($key, $val) = each( $array ) ) {
echo "$val | ";
}
echo "<br />\n";
}
echo( "</pre>" );
} elseif( $af = mysql_affected_rows( $link ) ) {
echo( "<p>$af rows affected.</p>\n" );
} else {
echo( "<p>Command completed.</p>\n" );
} } else {
echo( "<p>Bad query: '$sql;'.</p>" );
echo( mysql_errno() . ": " .
mysql_error() . "<br />" );
}
} else {
echo( "<p>Connected to '$DB'.</p>" );
} # end if
} else {
echo( "<p>Bad connection.</p>" );
echo( mysql_errno() . ": " .
mysql_error() . "<br />" );
}
?>
I was around the net and I get some info that don't give me help.
The solution from the net was to insert the value of the variable (in
this case $res) in a function like mysql_fetch_object or
mysql_fetch_row etc.

After some hours of trying to do something wtih those functions I
started to think that getting how much records I have in my DB is
almost a thing that only persons with a extremlly huge brain can
concive and only a few superior entities can made.
:)

Can some body with a huge brain help me?

Thank you in advance.
Auron

PS: I'm not a PRO I get only started to lear about PHP and MySQL.
because somebody have told me that "was" easy to make some thing with
those 2 tool.
http://eye.cc php newsgroups

--
Michael Austin.
DBA Consultant
Donations welcomed. Http://www.firstdbasource.com/donations.html
:)
Feb 21 '06 #2

P: n/a
You can also use PEAR DB to do this. IMO much easier than using
mysql_XXX function calls. I have some examples on my blog:
http://prepared-statement.blogspot.com

Alex

Feb 21 '06 #3

P: n/a
Hi Auron,

$result_count=mysql_query("SELECT COUNT(*) FROM my_data_table");
$count=mysql_result($result_count, 0)
echo $count;

After you run your COUNT query you want to fetch the first row of your
result.

- Clay

Feb 23 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.