Object can't be converted to a string in MySQLi PHP

后端 未结 3 1428
眼角桃花
眼角桃花 2020-12-12 07:20

Catchable fatal error: Object of class mysqli_result could not be converted to string in C:\\xampp\\htdocs\\xxx\\dash.php on line 20

I a

相关标签:
3条回答
  • 2020-12-12 07:46
    if ($result = $mysqli->query($query)) {
        while($row = $result->fetch_object()) {
                echo row['column_name'];
            }
    }
    $result->close();
    

    where you see 'column_name put the name of the column you want to get the string from.

    0 讨论(0)
  • 2020-12-12 07:49

    You have to fetch it first before echoing the results. Rough Example:

    function GetVar($var, $username, $mysqli) {
        // make the query
        $query = $mysqli->query("SELECT ".$var." FROM users WHERE username = '".$username."' LIMIT 1");
        $result = $query->fetch_assoc(); // fetch it first
        return $result[$var];
    }
    

    Then use your function:

    echo $user->GetVar('rank', 'Liam', $mysqli);
    

    Important Note: Since you're starting out, kindly check about prepared statements. Don't directly append user input on your query.

    0 讨论(0)
  • 2020-12-12 08:01

    Read this tutorial. it explain how to read and display data from a dataabse

    Select Data From a Database Table

    0 讨论(0)
提交回复
热议问题