kristi tanellari
kristi tanellari

Reputation: 135

Undefined offset: 0 error

I'm having this error on my code

Notice: Undefined offset: 0 in C:\xampp\htdocs\rekmovie\paginator.php on line 29

here is the full code

<?php 
$con=mysql_connect("localhost","root","");
$conn=mysql_select_db('rektechnologies'); 
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  } // include your code to connect to DB.
if (!empty($_GET["page"])) { $page  = $_GET["page"]; } else { $page=1; }; 
$start_from = ($page-1) * 20; 
$sql = "select * from videos order by id desc LIMIT $start_from, 10";
$rs_result = mysql_query($sql,$con); 
?> 
<table>
<tr><td>Name</td><td>Phone</td></tr>
<?php 
while ($row = mysql_fetch_assoc($rs_result)) { 
?> 
            <tr>
            <td><? echo $row["path"]; ?></td>
            <td><? echo $row["description"]; ?></td>
            </tr>
<?php 
}; 
?>
<?php 
$sql = "SELECT COUNT(*) FROM videos"; 
$rs_result = mysql_query($sql,$con); 
$row = mysql_fetch_assoc($rs_result); 
$total_records = $row[0];
$total_pages = ceil($total_records / 10);
for ($i=1; $i<=$total_pages; $i++) { 
    echo "<a href='paginator.php?page=".$i."'>".$i."</a> "; 
}; 
?>
</table>

can anyone help me with the error

Upvotes: 0

Views: 776

Answers (3)

iMakeWebsites
iMakeWebsites

Reputation: 587

change the query to SELECT COUNT(*) as some_alias FROM videos

and then use $total_records = $row['some_alias'];

Upvotes: 0

Barmar
Barmar

Reputation: 782785

Change

$row = mysql_fetch_assoc($rs_result); 

to:

$row = mysql_fetch_row($rs_result);

mysql_fetch_assoc returns an associative array, but you're trying to access $row[0], which expects an indexed array.

Upvotes: 2

Scott Jungwirth
Scott Jungwirth

Reputation: 6675

You are using mysql_fetch_assoc, but then trying to access the column name by integer

$row = mysql_fetch_assoc($rs_result);
$total_records = $row[0];

use either

$row = mysql_fetch_assoc($rs_result);
$total_records = $row['COUNT(*)'];

or

$row = mysql_fetch_array($rs_result);
$total_records = $row[0];

Upvotes: 0

Related Questions