Usign php and sql, I have been getting this error: Fatal error: Uncaught Error: Call to a member function fetch_assoc() on boolean in - Stack trace: #0 {main} thrown -
Here is my source code for any help:
<?php
session_start();
include 'dbh.php';
$uname=$_POST['uname'];
$pass=$_POST['pass'];
$sql="SELECT * FROM signup WHERE username='$uname' AND password='$pass'";
$result=$conn->query($sql);
if (!$row=$result->fetch_assoc()){
header("Location:error.php");
} else {
$_SESSION['name']=$_POST['uname'];
header("Location:home.php");
}
fetch_assoc()
will return's the set of values which we fetched from the query so for that you can't validate on true or false.
you can try this
$count = $result->fetch_row();
if ($count == 0){
header("Location:error.php");
} else {
$_SESSION['name']=$_POST['uname'];
header("Location:home.php");
}
When you call for fetch_assoc() you get an associated array returned to you with the row-name in the database as key. From what I see what you are trying to accomplish is to see weather or not you have a user with the matching credentials in your base. I would look at it this way.
$sql="SELECT * FROM signup WHERE username='$uname' AND password='$pass'";
$result = $conn->query($sql);
if($result->num_rows != 1){ //if there are 0 or more than 1 such user in the database
header("Location:error.php");
}else{
$_SESSION['name'] = $result->fetch_assoc()['name'];
}
Another thing I would recommend you to take a look at when it comes to php-script handling user information is "Prepared Statements" I will include a link here. https://www.w3schools.com/php/php_mysql_prepared_statements.asp
我想我们讨论了相同的代码:),我也遇到了相同的错误,但是解决方法是在index.php中给出登录名的第一种形式中添加method =“ post”
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.