dp19001 2013-03-07 03:59
浏览 62
已采纳

在PHP / SQLite中获取图像路径

I am attempting to get the path of an image in an SQLite database. This is my code:

<img src="<?php
    $database = new PDO("sqlite:database.sqlite");
    $database->query("SELECT path FROM images WHERE receiverId = '$_COOKIE['session']'");
?>" />

The table is images and the id of the user is stored in the cookie.

An image has 3 entries: id, senderId and receiverId. If the receiverId is the same as the cookie id (the user id), the image should be displayed. However, this code is not working. How can I fix it/recode it?

  • 写回答

1条回答 默认 最新

  • dongluo9156 2013-03-07 04:18
    关注

    You need to fetch the results of the query:

    <?php
    
      $database = new PDO('sqlite:database.sqlite');
      $r = $database->query('SELECT path FROM images WHERE receiverId = ' . $database->quote($_COOKIE["session"]));
      $v = $r->fetch(PDO::FETCH_ASSOC);
    
    ?>
    <img src='<?= $v["path"] ?>'>
    

    To make finding your problem easier, add a few debug statements:

    // ... create $database
    $database->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    // ... run the query (creating $r)
    var_dump($r);
    
    // ... fetch the results
    var_dump($v);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用