doulao5916 2017-11-26 04:06
浏览 14

如果执行查询,则显示查询结果

What I am trying to do is get the values of the $r variable (returns some vehicle id values) to display on the page. However it returns the following error.

Recoverable fatal error: Object of class mysqli_result could not be converted to string in C:\xampp\htdocs\SAMADHI\system\moduleeservation\controllereservationcontroller.php on line 166

The problem is on the echo $r statement. I tried echo '$r' but then it shows nothing even though it displays the the message 'vehicle available'.

if ($nor > 0) {
    $r = $objs->searchVehicle($vhandover, $vreturn, $seatcap);
    if ($r) {
        $msg = "Vehicle available";               
        $status = 1;
        echo $r;       
    } else {
        $msg = "Something is not right!";
        $status = 0;
    }
}

What am I doing wrong here and how can I correct it?

Thanks in advance :)

  • 写回答

1条回答 默认 最新

  • doufu1970 2017-11-26 04:21
    关注

    Let me assume, your vehicle table have following column - id, name, type. When your query executes, $r holds an associative array with search result-

    $r = [ ['id'=>1, 'name'=>"toyota", 'type'=> "regular"], ....]
    

    This is not any String. So if you want to echo any of the column value you need to mention it like -

    echo $r['name'];
    

    But if your query returns multiple results then, you need to put the echo inside a foreach loop.

    foreach($r as $row) {
        echo $row['name'];
    }
    

    UPDATE
    If the above code doesn't work for you then try the following-

    while($row = $r->fetch_assoc()) {
        echo $row['id'];
    }
    

    UPDATE: 2 You can use fetch_array($r) -

    while ($row = fetch_array($r)) {
        echo $row['name'];
    }
    

    Hope that clears your concept!

    评论

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助