Jump to content

Print each image when enter id


mark103

Recommended Posts

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?

Link to comment
Share on other sites

1. why are you casting a username into an integer? is it a number string?

2. This will grab whatever images you have with the username of $username, seems that you are going about it correctly, I don't really understand the issue here and what you mean by "random images not showing"..?

Link to comment
Share on other sites

well I have a mysql database where I am casing the username into an integer, so it will find the images whatever i stored in mysql. However, I do realised that I wouldn't need to as long the id and the images coumn name need to be necessary.

 

Sorry, I meant that the image did not changes when I enter with different id at the end of the url.

 

Any idea what I need to do to make some changes in order to get the images displaying with the correct id?

Link to comment
Share on other sites

I have already stored the query strings into mysql which I did get the results returned, so I don't know what to do and how to correct the situation as I am getting.

 

This is what I am trying to do:

enter the id at the end of the url something like this: http:www.mysite.com/images.php?id=123

each id i enter at the end of the url, it will enter to mysql database

find the id in mysql and find the header called images

print out each random image each time i enter the different id on the end of the url

 

 

do you know how to do this using with this 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']); 
} 
?>

Link to comment
Share on other sites

I believe something like this is what you will want...

 

if(isset($username)){
      $sql = "SELECT images FROM images_list WHERE username = '$username'";
      $result = mysql_query($sql) or die("Invalid query: " . mysqli_error());
      header("Content-type: image/jpeg");
      $im =  imagecreatefromstring(mysql_result($result, 0));
      imagejpeg($im);
      imagedestroy($im);
}

 

this is assuming that the image is a jpeg, but this is to give you the idea...

Link to comment
Share on other sites

thanks, i have a bit of trouble with the code. I cannot be able to get the output images as it will output the url link on my php page.

 

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

$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 user_channel_list WHERE username=$username";
  $result1=mysql_query($qrytable1) or die('Error:<br />' . $qry . '<br />' . mysql_error());

if(isset($username)){
      $sql = "SELECT id, images FROM images_list WHERE username = '$username'";
      $result = mysql_query($sql) or die("Invalid query: " . mysqli_error());
      header("Content-type: image/jpeg");
      $im =  imagecreatefromstring(mysql_result($result, 0));
      imagejpeg($im);
      imagedestroy($im);
} 
?>

 

any idea how to fix this?

Link to comment
Share on other sites

:facewall:

 

 while ($row = mysql_fetch_array($result1)) { 
   $image = $row['images']
   $details = getimagesize($image); 
   header ('Content-Type: ' . image_type_to_mime_type($details[2])); 
   readfile($image); //if fopen_wrappers is enabled
} 
     
} 

 

you will want to put the output php code into a separate php file...like "test.php", then your html will look like

<img src='test.php' />

 

untested..

Link to comment
Share on other sites

thanks, but there is a problem. i have got an error:

 

Parse error: syntax error, unexpected T_VARIABLE in /home/myusername/public_html/images.php on line 32

 

 

here is the line 32:

 

$details = getimagesize($image);

 

 

any idea how to correct the error?

Link to comment
Share on other sites

that is okay. i have added semi colon on the image = $row, however i am getting another error:

 

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 '' at line 1

 

 

here's the current code:

 

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


$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 image_list WHERE username=$username";
  $result1=mysql_query($qrytable1) or die('Error:<br />' . $qry . '<br />' . mysql_error());

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

 

 

what's wrong? :(

Link to comment
Share on other sites

i think i have found the solution. I have changed the id define instead of using the username define. every image have changes when i input each different id.

 

however, this is the last thing i need before this will get solve. Can you please help me how to set the html tag for this line?

 

echo "<p id='images'>", readfile($image);

 

I am getting the url output on my php page. So once we set the html tag, this will be solve.

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.