I am trying to retrieve 7 columns from my Pages table, however it comes up with this error: Wrong parameter count for mysql_fetch_assoc()
I must be using the wrong function, which would be the correct one to use?
Here is the code:
<?php
$query="SELECT * FROM Pages";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
?>
<?php
$array = array();
$i = 0;
while ($i < $num) {
$f1 = mysql_fetch_assoc($result,$i,"Page","URL","Description","Bounce_Rate",
"Avg_Time_On_Page","Page_Views","Click_Rate");
$array[] = $f1;
$i++;
}
echo json_encode($array);
?>
You run
mysql_fetch_assoc()on the result of the query:Note the signature of this function in the documentation:
This means the function returns an
arrayas its result. And it accepts only one parameter, which must be aresource. Aresourceis that which is returned frommysql_query. We know this by checking the signature of that method as well:This method returns a
resource(which can be used withmysql_fetch_assoc), and accepts at least one parameter (the query), with the optional second parameter of a link identifier (that is, your connection to a database).When in doubt, check the documentation. Also
mysql_numrowsshould bemysql_num_rows.