使用PHP如何显示已保存在SQL中的图像路径


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

我正试图从使用图像路径保存的SQL数据库中检索图像。映像保存在服务器上。当我检索图像时,没有返回任何内容。

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; 

代码示例用于不需要用户输入值的搜索。在SQL语句中,'URL'是存储映像路径的字段。图像路径值是完整的URL http://example.com/images/test.jpg,并存储为VARCHAR(200)。如有任何建议,不胜感激

您的代码应该不能正常工作。您不能关闭if语句。

同样,当$text不为空时,您不需要对数据库进行任何查询…

你需要这样写:

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];
}

顺便说一下,尝试使用PDO而不是mysql_query,最后一个是过时的,容易受到SQL注入。

图像在服务器中,所以路径只在服务器中起作用。

如果你做a:

echo json_encode($result);

我猜你正在将此信息返回给客户端,如果你想显示它们,图像路径没有值!

如果您想向用户显示图像,您需要使用HTML img标记并填充src属性的路径FROM the SERVER