Row count

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
digrev
Forum Newbie
Posts: 11
Joined: Sat Jun 19, 2010 1:51 pm

Row count

Post by digrev »

Hi eveyone this code is correct or not ?? because im gettin this error
not:i didnt write just copy paste and it works there

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\wamp\www\Page\index.php on line 7




<?php


$con=mysql_connect("localhost","root","") or die ("Mysql Error");
$db=mysql_select_db("ziyaretci") or die ("Connection Error");

$rec_count=mysql_fetch_array(mysql_query("SELECT count (*) from memberinfo"));



echo $rec_count;

?>
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: Row count

Post by Jade »

You have the wrong function. It should be:

Code: Select all

<?php


$con=mysql_connect("localhost","root","") or die ("Mysql Error");
$db=mysql_select_db("ziyaretci") or die ("Connection Error");

$rec_count=mysql_num_rows(mysql_query("SELECT count (*) from memberinfo"));

echo $rec_count;

?>
digrev
Forum Newbie
Posts: 11
Joined: Sat Jun 19, 2010 1:51 pm

Re: Row count

Post by digrev »

hi my friend plase someone check this link all the videos in this site are wrong or am i blind or an idiot????

http://www.seyretogren.com/video/php-de ... apmak.html
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Row count

Post by John Cartwright »

The way he used the functions were correct, just not recommended. The issue is when mysql_query() fails, it will return a boolean false, which is obviously not the resource mysql_fetch_assoc() is expecting.

Firstly, you should generally always have some form of error checking on your queries. The most common being

Code: Select all

$result = mysql_query("SELECT count (*) AS `count` from memberinfo") or die(mysql_error());
$rec_count = mysql_fetch_array($result);
You'll also notice I added an alias to more easily reference the aggregate function column.
Post Reply