dphdh395195 2013-04-02 10:59
浏览 39

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

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

  • 写回答

2条回答 默认 最新

  • duanhu2414 2013-04-02 11:06
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭