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.'"/>';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 输出区间内所有的完数的个数。 要简单点的程序
  • ¥15 asp.core 权限控制怎么做,需要控制到每个方法
  • ¥20 while循环中OLED显示中断中的数据不正确
  • ¥15 这个视频里的stm32f4代码是怎么写的
  • ¥15 串口发送数据和接收数据
  • ¥15 JNA调用DLL报堆栈溢出错误(0xC00000FD)
  • ¥15 请教SGeMs软件的使用
  • ¥15 自己用vb.net编写了一个dll文件,如何只给授权的用户使用这个dll文件进行打包编译,未授权用户不能进行打包编译操作?
  • ¥50 深度学习运行代码直接中断
  • ¥20 需要完整的共散射点成像代码