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

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'.

    已采纳该答案
    评论
    解决 无用
    打赏 举报