gpt4 book ai didi

php - 通过准备语句 mysqli php 得到不想要的结果

转载 作者:行者123 更新时间:2023-11-30 00:54:25 25 4
gpt4 key购买 nike

嗨,我是 PHP 新手,我正在尝试编写登录准备语句,但如下代码所示,我的查询没有获取任何行,但获取了 field_count

如果我这么说

SELECT * FROM users WHERE UserName = 'DemoUser'

在直接数据库中,我得到一行,但这里 $stmt->num_rows 为零。我缺少什么。

// in connections.php
$dbh = new mysqli($host, $username, $password, $database);

include('connections.php');
include('config.php');
include('comman_functions.php');

if(isset($_POST['password']) && $_POST['password']!=''
&& isset($_POST['username']) && $_POST['username']!=''
&& isset($_POST['location']) && $_POST['location']!='') {

$username = "DemoUser";
$stmt = $dbh->prepare("SELECT * FROM users WHERE UserName =? ");
$stmt->bind_param("s", $username);
$stmt->execute();
error_log($stmt->num_rows);
if($stmt->num_rows>0){
$row = $stmt->fetch();
error_log("logged in!!!");

$stmt->close();

} else {
$_SESSION['error'] = 'Invalid Username or Password';
header('Location:index.php');
}
} else {
$_SESSION['error'] = 'Please provide Username and Password';
header('Location:index.php');
}

最佳答案

试试这个!

需要在 $stmt->execute(); 之后添加 $stmt->bind_result();

$dbh = new mysqli($host, $username, $password, $database);// in connections.php



<?php
include('connections.php');
include('config.php');
include('comman_functions.php');

if(isset($_POST['password']) && $_POST['password']!='' && isset($_POST['username']) && $_POST['username']!='' && isset($_POST['location']) && $_POST['location']!=''){

$username = "DemoUser";
$stmt = $dbh->prepare("SELECT * FROM users WHERE UserName =? ");
$stmt->bind_param("s", $username);
$stmt->execute();
$stmt->bind_result();
error_log($stmt->num_rows);
if($stmt->num_rows>0){
$row = $stmt->fetch();
error_log("logged in!!!");

$stmt->close();

}else{
$_SESSION['error'] = 'Invalid Username or Password';
header('Location:index.php');
}
}else{
$_SESSION['error'] = 'Please provide Username and Password';
header('Location:index.php');
}

?>

关于php - 通过准备语句 mysqli php 得到不想要的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20709253/

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