hussnain aftab hussnain aftab -3 years ago 72
PHP Question

How do i navigate through images stored in mysql rather than displaying all of them?

This is my code but it displays all of the images saved in the database, i just want one to be displayed and the rest by using next and previous buttons. any help would be appreciated. Thanks!

$sql = "SELECT * FROM images";
$result = mysqli_query($conn, $sql) or die("bad request: $sql");
$i = 0;
while($row = mysqli_fetch_assoc($result)) {
if($i%3 == 0)
echo "<tr>";
echo"<td><img src='user_data/{$row['FILE_NAME']}' width=200 height=200></td>";
if($i%3 == 2) {

Answer Source

You should use LIMIT and OFFSET in your query. It would look like this:

$sql = "SELECT file_name FROM images LIMIT 0,1";

This will get you 1 record, starting at record 0. So from that query you can build your pagination script. Just keep adding one to the offset (the first number in the LIMIT clause). Remember to filter the page number (make sure it’s an int).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download