php mysqli_result 类的对象无法转换为字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2822302/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:46:36  来源:igfitidea点击:

Object of class mysqli_result could not be converted to string

phpmysql

提问by Joann

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 tell me what's causing the problem please???

如果我错过了一些愚蠢的事情,请原谅我。谁能告诉我是什么导致了问题???

回答by wash

The problem is that $uname is an object, not a string. You need to call one of $uname's methods to access the data.

问题是 $uname 是一个对象,而不是一个字符串。您需要调用 $uname 的方法之一来访问数据。

    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).

应该这样做(或上述解决方案之一)。

回答by Luis Melgratti

$unamereturned 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.

您的第一个查询返回的$uname是一个 mysql_result 对象,而不是一个字符串。您必须从该结果中获取数据才能在第二个查询中使用它。

while ($row = mysql_fetch_assoc($result)) {
    echo $row["usr"];
}

回答by Mitch Dempsey

The querymethod 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;

query方法返回查询结果的指针/对象,它不只是直接转储响应。你需要做类似的事情list($uname) = $uname->fetch_row;

回答by Phill Pafford

$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);