gpt4 book ai didi

php - 没有从 Mysqli 获取行

转载 作者:行者123 更新时间:2023-11-29 10:44:39 26 4
gpt4 key购买 nike

我正在使用mysqli来获取行,但它没有给我行,并且查询中没有错误。

$query="select * from members where useremail='$user_email' and password='$password'";
$result=$db->query($query);
$row = $db->fetch_array($result);
echo $row['id'];

我的查询功能

function query($query){
$result=mysqli_query($this->conn, $query);
if(!$result){
echo $this->err_msg = mysqli_error($this->conn);
return false;
}else{
return $result;
}
}

我的fetch_array函数

function fetch_array($result){
return mysqli_fetch_array($result);
}

如何使用 mysqli 获取 Row?

最佳答案

更改原始代码以反射(reflect)使用 mysqli 的绑定(bind)参数,这更安全并且应该可以工作

$query="select * from members where useremail='$user_email' and password='$password'";
$result=$db->query($query);
$row = $db->fetch_array($result);
echo $row['id'];

使用 mysqli 准备好的语句绑定(bind)参数

$query="select id from members where useremail=? and password=?";   // Don't use select *, select each column, ? are placeholders for your bind variables
$stmt = $connection->prepare($query);
if($stmt){
$stmt->bind_param("ss",$user_email,$password); // Bind in your variables, s is for string, i is for integers
$stmt->execute();
$stmt->bind_result($id); // bind the result to these variables, in the order you select
$stmt->store_result(); // Store if large result set, can throw error if server is setup to not handle more than x data
$stmt->fetch();
$stmt->close();
}
echo $id; // this would be same as $row['id'], $id now holds for example 5.

如果您选择多个内容,例如“SELECT id,name FROM...”,那么当您bind_result(..)时,只需将它们绑定(bind)在那里即可。 $stmt->bind_result($id,$name);

现在 $id 和 $name 保存与您的查询匹配的行的列数据。如果有多行匹配,您可以代替 $stmt->fetch()

while($stmt->fetch()){    // just like while($row = $result->fetch_assoc()){}
echo $id;
echo $name
}

关于php - 没有从 Mysqli 获取行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44848169/

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