Object of class mysqli_result could not be converted to string in

前端 未结 5 2088
遥遥无期
遥遥无期 2020-11-22 13:24

I am getting the error:

Object of class mysqli_result could not be converted to string

This is my code:

$result = mys         


        
5条回答
  •  长发绾君心
    2020-11-22 14:07

    mysqli:query() returns a mysqli_result object, which cannot be serialized into a string.

    You need to fetch the results from the object. Here's how to do it.

    If you need a single value.

    Fetch a single row from the result and then access column index 0 or using an associative key. Use the null-coalescing operator in case no rows are present in the result.

    $result = $con->query($tourquery);  // or mysqli_query($con, $tourquery);
    
    $tourresult = $result->fetch_array()[0] ?? '';
    // OR
    $tourresult = $result->fetch_array()['roomprice'] ?? '';
    
    echo 'Per room amount:  '.$tourresult;
    

    If you need multiple values.

    Use foreach loop to iterate over the result and fetch each row one by one. You can access each column using the column name as an array index.

    $result = $con->query($tourquery);  // or mysqli_query($con, $tourquery);
    
    foreach($result as $row) {
        echo 'Per room amount:  '.$row['roomprice'];
    }
    

提交回复
热议问题