I have three table register, profile and uploaddoc tables. In the profile table, I am inserting the multiple records of related registered users and in uploaddoc ,i am inserting the multiple records of the profile. At the end, I am getting the latest records from the profile and uploaddoc table.
I used the below code to get the output on my page. I am passing the profile id to the uploaddoc table to get the related records.
function profile($reg_id,$pdo){
$query='SELECT * FROM `tbl_profile` WHERE sp_id=(SELECT MAX(sp_id) FROM tbl_profile where reg_id ='.$reg_id.' GROUP BY reg_id order by reg_id DESC) and is_active=1';
$stmt = $pdo->prepare($query);
$stmt->execute();
$row = $stmt->fetch();
$data['profile']=$row;
if (!empty($row)) {
$query2='SELECT * FROM `tbl_uploadAll` WHERE u_id='.$row['sp_id'].' and is_active=1';
$stmt = $pdo->prepare($query2);
$stmt->execute();
$row2 = $stmt->fetchAll();
$data['uploadedDoc']=$row2;
}
return $data;
}
When the page loads then I am calling the profile function
<?php
$info=profile($_SESSION['reg_id'],$pdo);
$info['profile']; // getting single records
$info['uploadedDoc'] // getting the multiple records and displaying using for condition
?>
If the records are available on the profile and uploadedDoc then I am getting the data and it's working.
Now, My issue is, If there are no records in the profile and uploadedDoc table of related to register id then I am getting an error .
Notice: Trying to access array offset on value of type bool in
is there any way to solve this issue?
connection.php
<?php
session_start();
$hostname = "localhost";
$username = "root";
$password = "test";
$db="test";
$charset = 'utf8mb4';
$options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
$dsn = "mysql:host=$hostname;dbname=$db;charset=$charset";
try {
$pdo = new PDO($dsn, $username, $password, $options);
}
catch (PDOException $e) {
throw new PDOException($e->getMessage(), (int)$e->getCode());
}
I have modified the code:
$query='SELECT * FROM tbl_profile WHERE sp_id=(SELECT MAX(sp_id) FROM tbl_profile where reg_id =? GROUP BY reg_id order by reg_id DESC) and is_active=?'; $stmt = $pdo->prepare($query); $stmt->execute([$reg_id,$is_active]); $row = $stmt->fetch(); $data['profile']=$row; if (!empty($row)) { $query2='SELECT * FROM tbl_uploadAll WHERE u_id=? and is_active=?'; $stmt = $pdo->prepare($query2); $stmt->execute([$sp_id,$is_active]); $row2 = $stmt->fetchAll(); $data['uploadedDoc']=$row2; } return $data; }
Try and reference $pdo again as $pdo = $this->pdo; That's if you $pdo in the same class or use __construct
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.