Object of class mysqli_result could not be converted to string in

I am getting the error:

Object of class mysqli_result could not be converted to string

This is my code:

$username2 = htmlentities($_SESSION['user']['username'], ENT_QUOTES, 'UTF-8');

$con = mysqli_connect('localhost','root','','test');

$result = mysqli_query($con, "SELECT classtype FROM learn_users
                        WHERE username='$username2';");

echo "my result <a href='data/$result.php'>My account</a>";

Answers


The mysqli_query() returns an object resource to your $result variable, not a string.

You need to loop it up and then access the records. You just can't directly use it as your $result variable.

The code...
while ($row = $result->fetch_assoc()) {
    echo $row['classtype']."<br>";
}

Before using the $result variable, you should use $row = mysql_fetch_array($result) or mysqli_fetch_assoc() functions.

Like this:

$row = mysql_fetch_array($result);

and use the $row array as you need.


Try with:

$row = mysqli_fetch_assoc($result);
echo "my result <a href='data/" . $row['classtype'] . ".php'>My account</a>";

Make sure that mysqli_connect() is creating the connection to the DB. You can use mysqli_errno() to check for errors.


Need Your Help

Dynamic content is not displayed on page reload

javascript jquery html

After my page is loaded, I run a script where I dynamically add click event to my hidden button, and trigger the click. This way I am forcing a javascript library to reload every time a reload a pa...

how can i handle the jsp taglib by clicking event?

java onclick taglib

i have a form with one text input and submit button