1

i'm new in php coding, and a have a small problem. I wanna make more pages to be posted if there are more than 20 rows in a table. What is the problem exactly?

<?php
$sql = "SELECT COUNT ID FROM out_fact"; 
$query = mysql_query($sql); 
$result = mysql_fetch_row($query); 
$total_records = $result[0]; 
$total_pages = ceil($total_records / 20); 

for ($i=1; $i<=$total_pages; $i++) { 
            echo "<a href='view.php?page=".$i."'>".$i."</a> "; 
}; 
?>

If you need all the code, let me know. Thanks a lot!

  • possible duplicate of ["mysql\_fetch\_array() expects parameter 1 to be resource, boolean given" in select](http://stackoverflow.com/questions/2973202/mysql-fetch-array-expects-parameter-1-to-be-resource-boolean-given-in-selec) – kero May 07 '14 at 12:46
  • You're not bothering to check the success or otherwise of function calls that aren't garantueed to succeed. That's sloppy. For example you're not checking that mysql_query returned a resultset or False. If it returns False then the previous query failed. Also you shouldn't be using mysql_* because it's deprecated. – GordonM May 07 '14 at 12:50

2 Answers2

1

Query syntax should be,

SELECT COUNT(ID) FROM out_fact

Waring: Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Rikesh
  • 26,156
  • 14
  • 79
  • 87
  • Thank you so much, i'll give it a try to PDO, but i have to startover. I can't upvote since i don't have required reputation, otherwise i would'v. Thanks again! – Sorin Alexandru May 07 '14 at 12:51
1

count() is a function you are using count like a constant try

$sql = "SELECT COUNT(ID) FROM out_fact"; 

For more :- https://dev.mysql.com/doc/refman/5.0/en/counting-rows.html

Rakesh Sharma
  • 13,680
  • 5
  • 37
  • 44