tags:

views:

38

answers:

2

i am writing code of pagination & i got an error. this is my code :

<?php 
mysql_connect("localhost", "root", "") or die(mysql_error()); 
mysql_select_db("Admin") or die(mysql_error()); 

if (isset($_GET["page"]))
 {
 $page  = $_GET["page"]; 
 }
 else
 {
 $page=1;
 }



$start_from = ($page-1) * 2; 
$sql = "SELECT * FROM events ORDER BY event ASC LIMIT $start_from, 2"; 
$result = mysql_query ($sql) or die(mysql_error()); 
$num=mysql_numrows($result);
$x=0;
?> 
<table>
<tr><td>Event</td><td>Types</td></tr>
<?php 
while ($x<$num) {
$row1 = mysql_result($result,$x,'event');
$row2 = mysql_result($result,$x,'types'); 
?> 
            <tr>
            <td><? echo $row1; ?></td>
            <td><? echo $row2; ?></td>
            </tr>
<?php 
$x++;
} 
?> 
</table>
<?php 
$sql = "SELECT COUNT(event) FROM events"; 
$result = mysql_query($sql) or die(mysql_error()); 
$row = mysql_fetch_row($result); 
$total_records = $row[0]; 
$total_pages = ceil($total_records / 2); 

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

?>

now i got an error.

  object not found
+2  A: 

mysql_query returns false - i.e. your query failed and you are passing a wrong argument to mysql_num_rows. I think you have a typo in the table name in the SQL query, it should be events instead. Generally, you should check if the query succeeded:

$result = mysql_query ($sql) or trigger_error(mysql_error().": ".$sql);
dark_charlie
@dark_charlie hey dude there is nothing wrong in query or its naming.
Pratikg88
@dark_charlie hey dude i correct the mistake but it still say object not found
Pratikg88
Change to $result = mysql_query ($sql) or die(mysql_error()); as dark_charlie has suggested
Mark Baker
Pratikg88
Edited code to make it more reliable.
Col. Shrapnel
now i got that screen where 1 2 3 numbers(hyperlinks) are display. but when i click on it . it say object not found
Pratikg88
@Col. Shrapnel: Thanks. @Praktikg88: Please update the code to use trigger_error instead and then post the error message.
dark_charlie
+2  A: 

Note that instead of terrible while ($x<$num) you should use way more neat while($row=mysql_fetch_array($res))
so, make it

<?
$per_page = 2;
$page = 1;
if (isset($_GET['page'])) $page = $_GET['page'];
$start_from = ($page-1) * $per_page; 
$sql = "SELECT * FROM events ORDER BY event ASC LIMIT $start_from, $per_page"; 
$res = mysql_query ($sql) or trigger_error(mysql_error().": ".$sql);
?> 
<table>
  <tr><td>Event</td><td>Types</td></tr>
<?php 
while($row=mysql_fetch_array($res)) {
?> 
  <tr>
    <td><? echo $row['event'] ?></td>
    <td><? echo $row['types'] ?></td>
  </tr>
<?php 
} 
?> 
</table>
<?php 
$sql = "SELECT COUNT(event) FROM events"; 
$res = mysql_query ($sql) or trigger_error(mysql_error().": ".$sql);
$row = mysql_fetch_row($res); 
$total_records = $row[0]; 
$total_pages = ceil($total_records / $per_page); 

for ($i=1; $i<=$total_pages; $i++) { 
  echo "<a href='pagination.php?page=".$i."'>".$i."</a> "; 
}
?>
Col. Shrapnel
Pratikg88
@Pratik edited code
Col. Shrapnel