为什么无法使用PHP准备好的语句从SQL行获取数据?

问题描述 投票:0回答:1

连接良好。我可以插入数据库,并通过检查结果是否大于0来检查是否存在行,但是我无法选择行数据。正在测试的$ email在数据库中。例1。

require 'connection/connection.php';
$email = "[email protected]";

$sql = "SELECT * FROM users WHERE user_email=?"; // SQL with parameters
$stmt = $conn->prepare($sql); 
$stmt->bind_param("s", $email);
$stmt->execute();
$result = $stmt->get_result(); // get the mysqli result
$user = $result->fetch_assoc(); // fetch data  
echo $user['user_name'];

例如2

$email = "[email protected]";
$sql = "SELECT * FROM users WHERE user_email=?";
$stmt = mysqli_stmt_init($conn);
mysqli_stmt_bind_param($stmt, "s", $email);
mysqli_stmt_execute($stmt);

在每行一行接一个插入回声之后,就可以做到这一点。如果在下一行之后放置echo语句,它将不会出现。

$result = mysqli_stmt_get_result($stmt);
if ($row = mysqli_fetch_assoc($result)) {
    $_SESSION['active_user_id'] = $row['user_id'];
} else {
    header("Location: https://example.com/");
    exit();
}
php mysqli prepared-statement cpanel
1个回答
1
投票

问题已通过cPanel修复。我不得不从“ mysqli”切换到“ nd_mysqli”。这可以立即解决问题。

我在此处https://www.plus2net.com/php_tutorial/mysqli_mysqlnd.php找到了执行此操作的说明

我希望这可以帮助其他遇到相同问题的人。

© www.soinside.com 2019 - 2024. All rights reserved.