Object of class mysqli_result could not be converted to string error [duplicate]
I asked Google to help me I got no luck. :-( Here's the particular code that generates the error:
$this->conn->query("UPDATE tz_members SET confirm='yes' WHERE usr='".$uname."'");
The whole function is the following:
function update_confirm_field($code) {
$uname = $this->conn->query("SELECT usr FROM tz_members WHERE
confirm='".$code."'");
$this->conn->query("UPDATE tz_members SET confirm='yes' WHERE
usr='".$uname."'");
}
Forgive me if I have missed something stupid. Can anyone tel开发者_运维问答l me what's causing the problem please???
The problem is that $uname is an object, not a string. You need to call one of $uname's methods to access the data.
function update_confirm_field($code) {
$uname = $this->conn->query("SELECT usr FROM tz_members WHERE
confirm='".$code."'");
while ($row = $uname->fetch_assoc()) {
$this->conn->query("UPDATE tz_members SET confirm='yes' WHERE
usr='".$row["usr"]."'");
}
}
that should do it (or one of the above solutions).
$uname returned by your first query is a mysql_result object, not a string. you must fetch the data from that result in order to use it in your second query.
while ($row = mysql_fetch_assoc($result)) {
echo $row["usr"];
}
The query
method returns a pointer/object of the query result, it does not just directly dump the response. You need to do something like list($uname) = $uname->fetch_row;
$updateQuery = "UPDATE tz_members SET confirm='yes' WHERE usr= (SELECT usr FROM tz_members WHERE confirm='".$code."')";
// Get name and update in the same query
$this->conn->query($updateQuery);
精彩评论