简体   繁体   中英

Using PHP how can i display images which have been saved in SQL as a image path

I am trying to retrieve images from my SQL database which have been saved using the image path. The images are saved on the server. When i retrieve the images , nothing is returned.

if(isset($_GET))
{
  include_once "mysql_connect.php"; 
  $text = $_GET['text']; 


  if($text=="")
  {

  $query = mysql_query("SELECT `URL` FROM `gallery` WHERE `img_text` LIKE '1'")
  or die(mysql_error());  

  while( $rows=mysql_fetch_array($query) )
  {
   $search[] = $rows[0];

  }

 $result = array();
 $result["result"] = 500;
 $result["message"] = "Database Read Successfully";
 $result["rows"] = $search;
 echo json_encode($result);
 exit; 

The example of code is for a search without the user entering a value. Within the SQL statement 'URL' is the field where the image paths are stored.The images paths values are the complete URL http://example.com/images/test.jpg and stored as a VARCHAR(200) . Any advice would be appreciated

Your code should not work well. You are not closing the if statement.

Also, when $text is not empty you don't have any query to the database...

You would need something like:

if($text==""){
  $query = mysql_query("SELECT `URL` FROM `gallery` WHERE `img_text` LIKE '1'")
  or die(mysql_error());  
}

//when $text is not empty...
else{
  $query = mysql_query("SELECT `URL` FROM `gallery` WHERE `img_text` LIKE '".$text."'")
  or die(mysql_error());  
}

while( $rows=mysql_fetch_array($query)){
    $search[] = $rows[0];
}

By the way, try to use PDO instead of mysql_query , this last one is obsolete and is vulnerable to SQL injections.

The images are in the server, so the path only works there.

If you do a:

echo json_encode($result);

I guess you are returning this info to the client, the images path have no value there if you want to display them!

If you want to show the user the image you need to use a HTML img tag and fill the src attribute with the path FROM THE SERVER .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM