dre93205 2014-04-20 13:36
浏览 512
已采纳

警告:PHP中的非法字符串偏移量

I want to print out the data of users with an inner join but it won't work. The array is filled up correctly in the function and before the foreach but it keeps telling me:

Warning: Illegal string offset in PHP

THE ARRAY BEFORE THE FOREACH

array (size=6)
  'user_id' => string '1' (length=1)
  'name' => string 'Vandenbergh' (length=11)
  'surname' => string 'Jan' (length=3)
  'avatar' => string '1394041001.jpg' (length=21)
  'member_id' => string '21' (length=2)
  'group_id' => string '18' (length=2)

FUNCTION

public function getMembersInfo($group_id)
{
    var_dump($group_id);
    $db = new Db();
    $select = "SELECT u.user_id, u.name, u.surname, u.avatar, g.member_id, g.user_id, g.group_id FROM tblusers u INNER JOIN tblgroup_member g ON u.user_id = g.user_id WHERE g.group_id=" . $group_id .  "";

//  $result = $db->conn->query($select);
    //return $data=$result->fetch_assoc();

    $result = $db->conn->query($select);

$result_data = mysqli_fetch_assoc($result);
    var_dump($result_data);
return $result_data;

}

PHP

$memberinfo = $group->getMembersInfo($group_id);
var_dump($memberinfo);


 <?php 
        var_dump($memberinfo)   ;
      foreach ($memberinfo as $info) {
            echo "<p>" . $info['name'] . $info['surname'] . "</p>";

      }

        ?>
  • 写回答

1条回答 默认 最新

  • drelgkxl93433 2014-04-20 13:43
    关注

    You are only getting one array from your getMembersInfo method, not an array of arrays. So, when you write foreach ($memberinfo as $info), you're iterating over just one row from your database.

    Comment out the lines for yourself, and see:

    var_dump($memberinfo);
    //foreach ($memberinfo as $info) {
        $info = $memberinfo;
        echo "<p>" . $info['name'] . $info['surname'] . "</p>";
    //}
    

    To fix this, either remove the foreach loop, or append the results from your MySQL query inside the getMembersInfo() method into an array.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题