2

I am getting the errors:

Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in /home/www/thetotempole.ca/phpimageupload/pagecounter.php on line 19
title   bodytext

Warning: mysqli_fetch_row() expects parameter 1 to be mysqli_result, boolean given in /home/www/thetotempole.ca/phpimageupload/pagecounter.php on line 32

when I try to run my PHP page. I am expecting this problem is originating from either the $sql or the $connection. I don't believe it is my $connection because all of my variables are correct and I am not getting a connection error. The code is supposed to display my MySQL table's data four rows per page. After four rows have been displayed it will create a new page for the next four rows, and so on.

Here is my full PHP page's code:

<?php 
if (isset($_GET["page"])) { $page  = $_GET["page"]; } else { $page=1; }; 
$dbhost = 'ddm';
$dbuser = 'kdm';
$dbpass = 'Kder';
$dbname = 'kegbm';
$connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
if(! $connection )
{
  die('Could not connect: ' . mysqli_error());
}
$start_from = ($page-1) * 4; 
$sql = 'SELECT * FROM `testdb` ORDER BY `created` ASC LIMIT "'.$start_from.'",4';
$rs_result = mysqli_query ($connection, $sql); 
echo mysqli_error( $connection );
?> 
<table>
<tr><td>title</td><td>bodytext</td></tr>
<?php 
while ($row = mysqli_fetch_assoc($rs_result)) { 
?> 
            <tr>
            <td><? echo $row["title"]; ?></td>
            <td><? echo $row["bodytext"]; ?></td>
            </tr>
<?php 
}; 
?> 
</table>
<?php 
$sql = "SELECT COUNT(`created`) FROM `testdb`";
$rs_result = mysqli_query($connection, $sql); 
$row = mysqli_fetch_row($rs_result); 
$total_records = $row[0]; 
$total_pages = ceil($total_records / 4); 

for ($i=1; $i<=$total_pages; $i++) { 
            echo "<a href='pagination.php?page=".$i."'>".$i."</a> "; 
}; 
?>
Dharman
  • 30,962
  • 25
  • 85
  • 135
Kelsey
  • 913
  • 3
  • 19
  • 41
  • try including your connection variable before your select query – Dev Man Feb 19 '14 at 23:43
  • You need to verify that you have a valid mysqli_result before trying to operate against the result set. You are currently not doing any error handling at all. It would seem you are getting some kind of error and are not handling it. – Mike Brant Feb 19 '14 at 23:46
  • FYI - don't quote `LIMIT` arguments. They need to be numbers – Phil Feb 20 '14 at 00:06

2 Answers2

1

it's your solution :

$sql = "SELECT * FROM `testdb` ORDER BY `created` ASC LIMIT $start_from,4";

note:

in your code : before and after of $start_from has " that dont must !

DJafari
  • 12,955
  • 8
  • 43
  • 65
  • I have updated the code in my question and I am now getting the error `You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '"0",4' at line 1 ` on top of the other errors. – Kelsey Feb 19 '14 at 23:58
  • Got it working with the edited answer. Also, the MySQL table name had capitals in it which was also giving me connection problems because I didn't put the capitals in the PHP code. Thank you for your help! – Kelsey Feb 20 '14 at 15:15
0

The documentation states

Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or 
EXPLAIN queries mysqli_query() will return a mysqli_result object. For other 
successful queries mysqli_query() will return TRUE.

So your query is most likely failing for some reason. Find out what the raw SQL is, and you will probably be able to spot your error.

Justin Wood
  • 9,941
  • 2
  • 33
  • 46