Hi all,

I am working on the php script as I am using this for reading the images from each rows on mysql database. There are two rows that I have store in mysql already, so there is a little problem as I want you to take a look at this current code:

<?php
session_start();
    define('DB_HOST', 'localhost');
    define('DB_USER', 'mydbusername');
    define('DB_PASSWORD', 'mydbpassword');
    define('DB_DATABASE', 'mydbusername');

$username = (int)$_GET['username'];

$errmsg_arr = array();
$errflag = false;

$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link) {
    die('Failed to connect to server: ' . mysql_error());
}

$db = mysql_select_db(DB_DATABASE);
if(!$db) {
    die("Unable to select database");
}   

function clean($var)
{
    return mysql_real_escape_string(strip_tags($var));
}
  $qrytable1="SELECT images FROM images_list WHERE username=$username";
  $result1=mysql_query($qrytable1) or die('Error:<br />' . $qry . '<br />' . mysql_error());

 while ($row = mysql_fetch_array($result1)) { 
   $details = getimagesize($row['images']); 
   header ('Content-Type: ' . image_type_to_mime_type($details[2])); 
   echo  file_get_contents($row['images']); 
 } 
?>

There are two hotlinks images that I have stored in each row. When I entered the id at the end of the url, it will search the id in the database while it will look for the images through with the column name, the id and the username. The first image did printed on my php page when I entered the id, but the random images did not print out when I enter with different id. 🙁

Do you know where the trouble is and what I need to make some changes?

    I suspect the issue is that you can only output a single image from a given PHP script, and that is all it can output. It looks like you try to output multiple images in that while() loop?

      Yep, thats what I am trying to do. When I enter on the url bar something like this:

      http://www.mysite.com/myscript.php?id=1

      The first image to display while it looks for the row that has the id which is 1 and look on the same row while it looks for the header called images before it will print the right image.

      Do you know how to fix this?

        You will need to separate the logic that finds all the images to be displayed from the part that actually displays an image. The first part that finds all the images will be used to generate img tags in the HTML output, and the src attribute for each would contain the ID of the image to be displayed. So your query in the main file would get the primary key for each matching image instead of the actual image itself. Then in your while loop, assuming for now the PK field is called "image_id", it might be something like:

        while($row = mysql_fetch_assoc($result)) {
           printf("<img src='image.php?id=%d' alt='' />\n", $row['image_id']);
        }
        

        Then the image.php file (or whatever you want to call it) would grab the image data based on the id in the URL, e.g.:

        <?php
        $id = (!empty($_GET['id'])) ? (int)$_GET['id'] : 0;
        if($id) {
           // query the DB based on $id and display that image if found
        }
        
          Write a Reply...