1

我正在尝试从我的 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) 。任何意见,将不胜感激

4

2 回答 2

0

您的代码应该不能正常工作。你没有结束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 注入的攻击。

于 2013-04-02T11:06:06.643 回答
0

图像在服务器中,因此路径仅在那里有效。

如果您执行以下操作:

echo json_encode($result);

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

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

于 2013-04-02T11:07:40.233 回答