嗨,我是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');
}
答案 0 :(得分:0)
试试这个!
您需要在$stmt->bind_result();
$stmt->execute();
$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');
}
?>
答案 1 :(得分:0)
如果您在使用num_rows时遇到问题,则必须先声明->store_result()
。
<?php
$mysqli = new mysqli("localhost","root", "", "tables");
$query = $mysqli->prepare("SELECT * FROM table1");
$query->execute();
$query->store_result();
$rows = $query->num_rows;
echo $rows;