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

后端 未结 3 1438
眼角桃花
眼角桃花 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: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.

提交回复
热议问题