0

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\module\reservation\controller\reservationcontroller.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?

Dharman
  • 30,962
  • 25
  • 85
  • 135
Ru0912
  • 1

1 Answers1

1

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!

Erfan Ahmed
  • 1,536
  • 4
  • 19
  • 34
  • Hi again. Yes, my query returns multiple results. I tried this code as well. Still it didn't display anything... I even double checked the query. It works fine. – Ru0912 Nov 26 '17 at 04:42
  • It still did not work. i was wondering..., I don't have to get the id by some post or get method since it will be fetched by the query, do I? – Ru0912 Nov 26 '17 at 05:10
  • no, you don't. can you please post the code portion of `searchVehicalId()` – Erfan Ahmed Nov 26 '17 at 06:10
  • function searchVehicle($vhandover,$vreturn,$seatcap){ $con = $GLOBALS['con']; $sql = "SELECT v.v_id FROM vehicle v WHERE v.v_id NOT IN (SELECT r.v_id FROM reservation r, vehicle v WHERE (('$vhandover' between r.vhandover AND r.vreturn) OR ('$vreturn' between r.vhandover AND r.vreturn)) AND r.v_id=v.v_id AND v.vseatcap='$seatcap') AND v.vseatcap='$seatcap'"; $result = $con->query($sql); return $result; } – Ru0912 Nov 26 '17 at 07:20
  • then use `fetch_array($r)` inside `while` condition. – Erfan Ahmed Nov 26 '17 at 07:35
  • I tried all the given methods. but none of them are working.... but thank you so much for your help. – Ru0912 Nov 26 '17 at 12:19