我正在研究一个从数据库中检索登录详细信息以验证用户身份的项目。
但是,即使用户名和密码都在数据库中,我也无法获得身份验证部分,但是检索它们时还是有问题。另外,我得到了错误“试图获取非对象的属性num_rows”。请告知什么是非对象。
提前谢谢!
下面是我的代码段:
<?php //login.php
include "dbconnect.php";
session_start();
if (isset($_POST['username']) && isset($_POST['password']))
{
// if the user has just tried to log in
$username = $_POST['username'];
$password = $_POST['password'];
$password = md5($password);
$query = 'select * from users'
."where username='$username' "
." and password='$password'";
//echo "<br>" .$query. "<br>";
$result = $dbcnx->query($query);
if ($result->num_rows >0 )
{
// if they are in the database register the user id
$_SESSION['valid_user'] = $username;
}
$dbcnx->close();
}
?>
<?php
@$dbcnx = new mysqli('localhost','root','','kimbae');
if ($dbcnx->connect_error){
echo "Database is not online";
exit;
}
if (!$dbcnx->select_db ("kimbae"))
exit("<p>Unable to locate the kimbae database</p>");
?>
答案 0 :(得分:0)
您提到的Trying to get property num_rows of non-object
错误与$result
没有收到正确的查询结果有关。您可以echo $result;
看看那里有什么。可能为空或为空。
假设$dbcnx
存在且正确,我注意到的唯一错误是您的查询字符串在第二行缺少空格,这将创建无效的SQL查询。应该是这样的:
$query = 'select * from users'
." where username='$username' "
." and password='$password'";
如果这行不通,请同时发布您在 dbconnect.php 中拥有的代码。
PS 。:即使我不是PHP专家,您也可能应该研究一种更好/更安全的身份验证方法。也许使用框架之类的东西。
PS 2 ::与javascript无关,因此标记错误:)