Catchable fatal error: Object of class mysqli coul

2020-02-07 13:41发布

问题:

So I have a bit of a problem, I'm EXTREMELY new to PHP and I'm having a problem updating a string on my database, It's throwing out this error: Catchable fatal error: Object of class mysqli could not be converted to string in C:\xampp\htdocs......\ban.php on line 11

Here is the source:

<?php
$servername = "localhost";
$username = "example";
$password = "example";
$name = "Brendan";

// Create connection
$conn = new mysqli($servername, $username, $password);
$query = mysqli_query("$conn, UPDATE * wp_oxygenpurchaseusers
                  SET user_url =  '2'
                  WHERE display_name = $name");
                    while ($row = $result->fetch_assoc()) {
                        echo $row['classtype']."<br>";
}
?>

Thanks in advance for everyone's help :)

回答1:

There are several problems with your code.

  • First of all, you are assigning the variable $query, but using $result when trying to fetch the result.
  • Secondly, you have included your connection ($conn), inside your query. It needs to be separated.
  • Finally, you probably need to put quotes around your value.

    $result = mysqli_query($conn, "UPDATE * wp_oxygenpurchaseusers
                       SET user_url =  '2'
                       WHERE display_name = '$name'");
    

If user_url is always a number, you should really convert it to type INT instead of using a string.



回答2:

$query = mysqli_query("$conn, UPDATE ");

you are passing one solid string into function, instead of 2 separate parameters. * is superfluous too.

I WISH there was a method to remove this kind of questions right after the OP read the answer.

Instead of leaving it to hang around forever with 1 or 2 occasional votes.



标签: php mysql mysqli