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条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题