所以我试图从我的MySQL数据库中获取一些数据,但我继续收到此错误:
注意:尝试在第28行获取非对象的属性
这是我的代码:
$uname = $_POST['uname'];
$upass = $_POST['upass'];
$servername = "<hostname>";
$username = "<user>";
$password = "<pass>";
$dbname = "<dbname>";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT username, password FROM users WHERE username='".$uname."' AND password='".$upass."'";
$result = $conn->query($sql);
if ($result->num_rows == 1) {
while($row = $result->fetch_assoc()) {
$_SESSION['uname'] = $row['username'];
}
} else {
echo "Incorrect Details";
}
//close connection
$conn->close();
答案 0 :(得分:0)
你得到那个错误是因为:
1.您的查询失败。如果函数查询失败,则返回false。您的数据库中是否有名为users的用户名和密码字段?完全打字,尊重首都?
2.您的连接无效。通过插入一些调试消息来检查您的连接是否正常工作
答案 1 :(得分:0)
你需要改变这个
$conn = new mysqli($servername, $username, $password);
到这个
$conn = new mysqli($servername, $username, $password, $dbname);
请参阅this