dongnao9525 2014-10-06 20:25
浏览 231
已采纳

MySQL查询预处理语句不返回任何结果

When I am running this query it is not returning any results. I believe there must be a syntax error as it is stopping the code from running later down the page. Anyway, I can't see where I am going wrong.

If nothing seems to be the problem with the syntax I can post my database structure, if necessary.

    $tag = $_GET['tag'];
    $stmt = $mysqli->prepare('SELECT trips.trip_id FROM tags JOIN trips ON trips.post_id = tags.post_id WHERE tag = ?');
    $stmt->bind_param('s', $tag);
    $stmt->execute();
    $stmt->bind_result($trip_id); ?>

    <div id="map_canvas<?php echo $trip_id ?>" style="height:400px;"></div><?php

Update: I have ran the error reporting script and this is what is comes out with...

Fatal error: Call to a member function bind_param() on a non-object in /Users/.../server/inc/feed-algorithm.php on line 37
  • 写回答

1条回答 默认 最新

  • doyhq66282 2014-10-06 20:59
    关注

    Look at PHP manual http://php.net/manual/en/mysqli.quickstart.prepared-statements.php and set up your error statements properly. Your prepare statement should look like this:

    if(!$stmt = $mysqli->prepare('SELECT trips.trip_id FROM tags JOIN trips ON trips.post_id = tags.post_id WHERE tag = ?')){
        echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;
    }
    

    Of course, alternatively, throw and handle exceptions if you would like to be a little more elegant and keep your errors from users.

    Also follow error checking in the Example 2 section. It appears your problem is going to be in the prepare statement though because if it was successful, it would not return a 'non-object'.

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

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀