问题
Everything seems to be working good but i am getting an error:
I checked for typos in my follow.php and profile.php and even retyped the major part of the code but was not able to figure out the problem. This is my line 25 of follow.php
if($data['reciever'] == $profileID)
Here is my follow.php
<?php
class Follow extends User
{
function __construct($pdo)
{
$this->pdo = $pdo;
}
public function checkFollow($followerID, $user_id)
{
$stmt = $this->pdo->prepare("SELECT * FROM `follow` WHERE `sender` = :user_id AND `reciever` = :followerID");
$stmt->bindParam(":user_id", $user_id, PDO::PARAM_INT);
$stmt->bindParam(":followerID", $followerID, PDO::PARAM_INT);
$stmt->execute();
return $stmt->fetch(PDO::FETCH_ASSOC);
}
public function followBtn($profileID, $user_id)
{
$data = $this->checkFollow($profileID, $user_id);
if($this->loggedIn() === true)
{
if($profileID != $user_id)
{
if($data['reciever'] == $profileID)
{
//Following Button
echo "<button class='f-btn following-btn follow-btn' data-follow='".$profileID."'>Following</button>";
}
else
{
//follow button
echo "<button class='f-btn following-btn follow-btn' data-follow='".$profileID."'><i class='fa fa-user-plus'></i>Follow</button>";
}
}
else
{
//edit button
echo "<button class='f-btn' onclick=location.href='profileEdit.php'>Edit Profile</button>";
}
}
else
{
echo "<button class='f-btn' onclick=location.href='index.php'><i class='fa fa-user-plus'></i>Follow</button>";
}
}
}
?>
The error states:
Trying to access array offset on value of type bool
and this is happening on line 25 of follow.php
So, On looking at line 25 of follow.php
if($data['reciever'] == $profileID)
According to the error, we are trying to access an element in an array but the object is actually a boolean. So, $data must be a boolean. Looking a few lines up, on line 20 there is:
$data = $this->checkFollow($profileID, $user_id);
So $data is being set by the return value of checkFollow. checkFollow must be giving us a boolean. Looking at checkFollow we see it returns this:
return $stmt->fetch(PDO::FETCH_ASSOC);
the boolean must be coming from $stmt->fetch. Let's look at the documentation for this method, which states:
The return value of this function on success depends on the fetch type. In all cases, FALSE is returned on failure.
My DB:
So that's the source of the error. Why is fetch returning FALSE though?Maybe query is not returning any results. Perhaps there are no rows in the database that match the given parameters.But I don't know how to implement it as i am just beginning in PHP.If someone could show me the syntax for better clarification it would be highly appreciated.! Thank you!
来源:https://stackoverflow.com/questions/63770388/notice-trying-to-access-array-offset-on-value-of-type-bool-in-c-xampp-htdocs-t