What I am trying to do is get the values of the $r variable (returns some vehicle id values) to display on the page. However it returns the following error.
Recoverable fatal error: Object of class mysqli_result could not be converted to string in C:\xampp\htdocs\SAMADHI\system\moduleeservation\controllereservationcontroller.php on line 166
The problem is on the echo $r
statement. I tried echo '$r' but then it shows nothing even though it displays the the message 'vehicle available'.
if ($nor > 0) {
$r = $objs->searchVehicle($vhandover, $vreturn, $seatcap);
if ($r) {
$msg = "Vehicle available";
$status = 1;
echo $r;
} else {
$msg = "Something is not right!";
$status = 0;
}
}
What am I doing wrong here and how can I correct it?
Thanks in advance :)
Let me assume, your vehicle table have following column - id, name, type
. When your query executes, $r
holds an associative array with search result-
$r = [ ['id'=>1, 'name'=>"toyota", 'type'=> "regular"], ....]
This is not any String
. So if you want to echo
any of the column value you need to mention it like -
echo $r['name'];
But if your query returns multiple results then, you need to put the echo
inside a foreach
loop.
foreach($r as $row) {
echo $row['name'];
}
UPDATE
If the above code doesn't work for you then try the following-
while($row = $r->fetch_assoc()) {
echo $row['id'];
}
UPDATE: 2 You can use fetch_array($r)
-
while ($row = fetch_array($r)) {
echo $row['name'];
}
Hope that clears your concept!