gpt4 book ai didi

php - 如何在 mysqli 中获取结果/行

转载 作者:行者123 更新时间:2023-11-29 00:02:23 26 4
gpt4 key购买 nike

我正在学习 mysql,但我不确定这段代码中接下来要用什么来获取匹配的行作为结果,我该如何获取它?

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

我相信这对你来说很简单,请帮助我学习一些东西。

最佳答案

查看 this page 上的示例 #6|

这是相关的部分,就在 bind_param() 和 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)\n", $out_id, gettype($out_id), $out_label, gettype($out_label));
}

因为我不知道您使用 SELECT * 拉入了哪些列,所以我将保留示例。

您还可以使用 fetch_assoc(),它可能更容易使用,并且最终被更广泛地使用。从 same page 挖掘在 php.net 上,我们从示例 #8 中获得了这个相关部分(实际上我不得不对其进行一些修改 - 示例 #8 正在使用缓冲做一些时髦的事情):

if (!($res = $stmt->get_result())) {
echo "Getting result set failed: (" . $stmt->errno . ") " . $stmt->error;
}

while ($row = $res->fetch_assoc()) {
var_dump($row);
}
$res->close();

关于php - 如何在 mysqli 中获取结果/行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29199541/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com