dongmaomou4117 2015-03-22 20:35
浏览 72
已采纳

如何在mysqli中获取结果/行

I am learning mysql but I am not sure what is next going to be here in this code to get the matching row as result, how can I fetch that?

$stmt= $db->prepare("SELECT * FROM users WHERE email=? and password = ?");
$stmt->bind_param("ss", $_POST['user_name'],md5($_POST['user_pass']));
$stmt->execute();

I am sure this is a simple one for you, please help me out in learning things.

  • 写回答

1条回答 默认 最新

  • douyanjing0822 2015-03-22 20:41
    关注

    Take a look at example #6 on this page

    Here's the relevant bit, right after the bind_param() and the execute():

    $out_id    = NULL;
    $out_label = NULL;
    if (!$stmt->bind_result($out_id, $out_label)) {
        echo "Binding output parameters failed: (" . $stmt->errno . ") " . $stmt->error;
    }
    
    while ($stmt->fetch()) {
        printf("id = %s (%s), label = %s (%s)
    ", $out_id, gettype($out_id), $out_label, gettype($out_label));
    }
    

    Since I don't know what columns you are pulling in with your SELECT * I'll just leave the example as it stands.

    You can also use fetch_assoc() which is probably a little easier to use and ends up being used a lot more commonly. Digging from the same page on php.net we get this relevant portion from example #8 (actually I had to modify it somewhat - example #8 is doing some funky stuff with buffering):

    if (!($res = $stmt->get_result())) {
        echo "Getting result set failed: (" . $stmt->errno . ") " . $stmt->error;
    }
    
    while ($row = $res->fetch_assoc()) {
        var_dump($row);
    }
    $res->close();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求计算赫斯特(Hurst)指数
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大