duanlipeng4136 2015-09-07 19:52
浏览 67
已采纳

使用mysqli的SELECT返回null

I'm trying to select a row from a table using mysqli but all I can get is a bunch of null values and I don't really know why. The same query works using normal php mysql and if I try to perform the same query on phpMyAdmin using the parameter I pass goes through fine.

Here's the code:

$con = mysqli_connect('localhost', 'user', 'pass',"db");

if (mysqli_connect_errno()){
    die("Failed to connect to MySQL: " . mysqli_connect_error());
}
$coupon = $_GET['coupon'];
$sql = mysqli_prepare($con, "SELECT * FROM coupon WHERE coupon=?");
$sql->bind_param('s', $coupon);
$sql->execute();
$sql->store_result();
echo $sql;

returns

"affected_rows":null,
"insert_id":null,
"num_rows":null,
"param_count":null,
"field_count":null,
"errno":null,
"error":null,
"error_list":null,
"sqlstate":null,
"id":null

I already tried to search for an answer either here and on google but I couldn't find anything close to my problem. What am I doing wrong?

  • 写回答

2条回答 默认 最新

  • duan117890 2015-11-03 09:29
    关注

    author's final solution (moved from question content):

    As suggested by user @RiggsFolly I wasn't fetching the results at all, plus I was mixing procedural and OOP approaches as suggested by user @Alex . Here's the working code for future reference to anyone who will arrive here with a similar problem:

    $coupon = $_GET['coupon'];
    if ($sql = mysqli_prepare($con, "SELECT * FROM coupon WHERE coupon=?;")){
        mysqli_stmt_bind_param($sql, 's', $coupon);
        mysqli_stmt_execute($sql);
        mysqli_stmt_bind_result($sql, $ID, $coupon, $discount, $uses);
        mysqli_stmt_fetch($sql);
        $data = array(
            'ID' => $ID,
            'coupon' => $coupon,
            'discount' => $discount,
            'uses' => $uses
        );
        echo json_encode($data);
    }else{
        echo json_encode(FALSE);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!