【发布时间】:2014-01-09 15:33:18
【问题描述】:
您好,我是 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->num_rows 上。见stackoverflow.com/questions/5556540/…
-
我正在使用 mysqli $dbh = new mysqli($host, $username, $password, $database);