Page 1 of 1

Row count

Posted: Wed Jun 23, 2010 11:31 am
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;

?>

Re: Row count

Posted: Wed Jun 23, 2010 11:43 am
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;

?>

Re: Row count

Posted: Wed Jun 23, 2010 11:51 am
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

Re: Row count

Posted: Wed Jun 23, 2010 4:28 pm
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.