简体   繁体   中英

Why can't I get data from SQL row using PHP prepared statements?

Connection is good. I can insert into the database, and check if a row exists by checking if results > 0, but I can not select row data. The $email's being tested are in the database. Ex 1.

require 'connection/connection.php';
$email = "sample@sample.com";

$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'];

Ex. 2

$email = "james@james.com";
$sql = "SELECT * FROM users WHERE user_email=?";
$stmt = mysqli_stmt_init($conn);
mysqli_stmt_bind_param($stmt, "s", $email);
mysqli_stmt_execute($stmt);

After inserting an echo after every line one by one, this is as far as it gets. If an echo statement is placed after the next line it will not appear.

$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();
}

The problem was fixed through cPanel. I had to switch from "mysqli" to "nd_mysqli." This fixed the problem right away.

I found the instructions to do this here https://www.plus.net.com/php_tutorial/mysqli_mysqlnd.php

I hope this helps others with the same problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM