dongxi1320 2016-02-19 19:06
浏览 50
已采纳

为什么我会收到警告:使用PDO :: fetchColumn时非法字符串偏移?

I'm trying to output a user profile image or display a dummy image if the user hasn't uploaded a profile picture. The query that pulls this info from the database works fine. I've used an 'if' statement to check whether the row contains a value, and this works until I attempt to echo something from the database, e.g. a file path. Then I get this error:

Warning: Illegal string offset

Also the image does not display, I get

Object not found

Please see my code below. I would appreciate any advice.

$query = $db->query("SELECT * from members where username = '".$_SESSION['username'] ."'");

$row = $query->fetchColumn();

if (isset($row) && $row == true) {
    echo '<img width="100" height="100" src="'.$row["profile_pic"].'"/>';
} else {
    echo "no profile pic";
}
  • 写回答

1条回答 默认 最新

  • doushi2047 2016-02-19 19:21
    关注

    The benefit of using fetchColumn() is that it returns a single value. This can slightly simplify your code by removing the necessity to reference an array key if you only need the value from one column. It isn't working because you're using $row["profile_pic"] and $row is not an array.

    If you're going to use fetchColumn(), you need to do one of two things:

    1. Modify your query so that it only fetches the column you want, rather than using *. Then your code will need to be

      $query = $db->query("SELECT profile_pic from members...");
      
      $src = $query->fetchColumn();
      if ($src) {
          echo '<img width="100" height="100" src="'.$src.'"/>';
      
    2. Add a number to fetchColumn() that indicates which column you want to fetch. Then your code will need to be

      $query = $db->query("SELECT * from members...");
      
      $src = $query->fetchColumn(4);
      // or whatever the numeric index of the column you want is
      if ($src) {
          echo '<img width="100" height="100" src="'.$src.'"/>';
      

    Either of these should work. But I should also mention that if you want to use any other columns from your query, you should NOT use fetchColumn(), because once you have done this, you will not be able to access any other columns from that row of your query result. If this is the case, you can change your fetch from

    $row = $query->fetchColumn();
    

    to

    $row = $query->fetch(PDO::FETCH_ASSOC);
    

    Then you will still be able to get other columns from the row, and you can still use $row["profile_pic"] as you do in your original code.


    To address your comment, the way I would suggest doing this in order to display a default image if there isn't one in your database is to set $src to the default image path if it is empty, like this:

    $src = $query->fetchColumn();
    if (!$src) {
        $src = 'default_image.png'; // or whatever the path/filename is
    }
    echo '<img width="100" height="100" src="'.$src.'"/>';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan