0

我尝试使用函数查询获得一个非常简单的查询的结果,但没有出现。如果我在 PHPMyAdmin 中使用相同的数据执行查询,我就会得到结果。

有我的代码:

$sql = "SELECT * FROM users WHERE email='$email'";

$response = $conn->query($conn, $sql);

$conn 变量是正确的,我用它做了一个插入。

$response 为空。我可以做一个回声,什么都没有。

我能做些什么来解决这个问题?我可以检查什么?

非常感谢。

4

2 回答 2

4

您不需要在查询中传递连接。

解决方案:

$sql = "SELECT * FROM users WHERE email='$email'";
$response = $conn->query($sql);
while($res = $response->fetch_array()){
$name=$res['nameofuser']; //just an example
}
echo $name;

真正的解决方案(准备stmt):

$sql = "SELECT * FROM users WHERE email=?";
$response = $conn->prepare($sql);
$response->bind_param('s',$email);
if(!$response->execute()){
echo "Error query: " . $response->error . ".";
}
$result=$response->get_result();
while($res = $result->fetch_array()){
$name=$res['nameofuser']; //just an example
}
echo $name;

'提示'添加到真正的解决方案检查查询是否完成。

于 2020-02-20T13:15:54.203 回答
1

执行查询后。获取结果

   $stmt = $conn->prepare( "SELECT * FROM users WHERE email= ? ");

$stmt->bind_param("s", $email);
$stmt->execute();
$result = $stmt->get_result();
if($result->num_rows === 0) exit('No rows');
while($row = $result->fetch_assoc()) {
// your code
}
于 2020-02-20T13:16:14.327 回答