gpt4 book ai didi

PHP使用存储过程获取从数据库返回的结果数

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

我正在尝试使用 mysqli_num_rows 从数据库获取结果数,但我不知道要传入什么参数,因为我正在使用存储过程。我将发布我所做的事情。在我当前的代码中,执行了 Else。我是 PHP 新手。

<?php

session_start();

include_once 'constants.php';
include_once 'connection.php';

$connection = new database();

$connection->connect();

$email = $_POST['email'];
$password = $_POST['password'];

// Call the login stored procedure
$conn = $connection->query("CALL LoginUser('$email','$password')");

//Check if any users match
$rows = mysqli_num_rows($conn);
if($rows > 0){

// If result was found put information into array
$user = mysqli_fetch_assoc($conn);

//Store all the users info in session variables
$_SESSION['user_id'] = $user['account_id'];
$_SESSION['user_firstName'] = $user['fname'];
$_SESSION['user_lastName'] = $user['lname'];
$_SESSION['user_password'] = $user['password'];
$_SESSION['user_dob'] = $user['dob'];
$_SESSION['user_joinDate'] = $user['joinDate'];
$_SESSION['user_picture'] = $user['picture'];

//Redirect user to logged in home page
header('Location: home.php');

// If no results are found redirect user to noAccount page
} else {
header('Location: noAccount.php');
}


?>

最佳答案

$user = mysqli_fetch_assoc($conn);
if(!empty($user) > 0){

//Store all the users info in session variables
$_SESSION['user_id'] = $user['account_id'];
$_SESSION['user_firstName'] = $user['fname'];
$_SESSION['user_lastName'] = $user['lname'];
$_SESSION['user_password'] = $user['password'];
$_SESSION['user_dob'] = $user['dob'];
$_SESSION['user_joinDate'] = $user['joinDate'];
$_SESSION['user_picture'] = $user['picture'];

//Redirect user to logged in home page
header('Location: home.php');

// If no results are found redirect user to noAccount page
} else {
header('Location: noAccount.php');
}

mysqli_num_rows 正在等待输出结果。

编辑:num_rows 期望查询结果而不是获取的结果。在这种情况下,我建议您使用类似我修改的上述代码。

关于PHP使用存储过程获取从数据库返回的结果数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47618358/

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