Jump to content

Displaying images from result set


oha055

Recommended Posts

Hi!

 

I want the image of my site to be clickable so that it shows the next image contained in the result set of the sql query.

 

<?php
require_once('includes/connection.inc.php');
$conn = dbConnect();
$sql = 'SELECT filename FROM images ORDER BY image_id DESC LIMIT 1';
$msg = '';
if(!$sql) { echo "Something went wrong with the query. Please try again."; }
$result = $conn->query($sql) or die(mysqli_error());
if(mysqli_num_rows($result) == 0) {
header('Location: empty_blog.php');
$conn->close();
} else {
$row = $result->fetch_row();
$image = $row[0];
}
?>
<?php include('header.php'); ?>
<div class="content main">
<img id="image" src="images/<?php echo $image; ?>"/>
</div>
<?php include('includes/footer.inc.php'); ?>

 

All the filenames of the images are contained correctly in the $result variable, all I need to do now is to fetch the next image in the set. How would I go about this? Any help is greatly appreciated! :)

Link to comment
Share on other sites

Try the following:

 

<?php

$imageid = 1;

if(isset($_GET['id'])
{
    $imageid = $_GET['id'];
}

require_once('includes/connection.inc.php');
$conn = dbConnect();
$sql = 'SELECT filename FROM images WHERE image_id = $imageid';
$msg = '';
if(!$sql) { echo "Something went wrong with the query. Please try again."; }
$result = $conn->query($sql) or die(mysqli_error());
if(mysqli_num_rows($result) == 0) {
header('Location: empty_blog.php');
$conn->close();
} else {
$row = $result->fetch_row();
$image = $row[0];
}
?>
<?php include('header.php'); ?>
<div class="content main">
<a href="thispage.php?id=<?php echo $imageid+1; ?>">
<img id="image" src="images/<?php echo $image; ?>"/>
</a>
</div>
<?php include('includes/footer.inc.php'); ?>

 

I haven't actually tested this so forgive me if I've missed something out. Hopefully you'll get the idea.

Link to comment
Share on other sites

Thank you so much for your reply! :)

 

I tried out your suggestion, but now I am getting this error:

 

"Warning: mysqli_error() expects exactly 1 parameter, 0 given in C:\xampp\htdocs\photoblog\index.php on line 13"

 

On this line:

 

$result = $conn->query($sql) or die(mysqli_error());

 

Don't know what to make of this, there was no bitching about this before  :P

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.