php 可捕获的致命错误: mysqli 类的对象无法转换为字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19245473/
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
Catchable fatal error: Object of class mysqli could not be converted to string in what is ment buy it beind an object?
提问by Nangu Haci
I have been on the site for some time now and I can't seem to get the idea that most of the similar questions are getting answers for this error:
我已经在这个网站上呆了一段时间了,我似乎无法理解大多数类似问题都得到了这个错误的答案:
Catchable fatal error: Object of class mysqli could not be converted to string
可捕获的致命错误:无法将 mysqli 类的对象转换为字符串
Saying it is an object. I am fairly new to PHP and it would really help if anyone could explain this to me. I am trying to retrieve data from my database and echo it in a table.
说它是一个对象。我对 PHP 相当陌生,如果有人能向我解释这一点,那真的很有帮助。我正在尝试从我的数据库中检索数据并将其回显到表中。
This is what I have done so far:
这是我到目前为止所做的:
$dbcon=mysqli_connect("localhost","root","","technoage");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$results = mysql_query("SELECT * FROM items WHERE item_id = 1,$dbcon");
if(!$results)
{
die("Database query failed".mysql_error());
}
while($row = mysql_fetch_array($results))
{
echo $row['descreption']." ".$row['price']."<br/>";
}
回答by MaxEcho
You are connected with mysqli
你与 mysqli
but your are querying with mysql
但你正在查询 mysql
$results = mysql_query("SELECT * FROM.....
回答by echo_Me
rewrite your code with mysqli not mysql as you are mixing mysqli with mysql.
使用 mysqli 而不是 mysql 重写您的代码,因为您正在将 mysqli 与 mysql 混合。
this
这个
$results = mysql_query("SELECT * FROM items WHERE item_id = 1,$dbcon");
should be
应该
$results = mysqli_query($dbcon, "SELECT * FROM items WHERE item_id = 1");
and this
和这个
while($row = mysql_fetch_array($results))
should be
应该
while($row = mysqli_fetch_array($results))
回答by MrCode
This error is not caused by mixing mysql and mysqli. It's caused by inserting $dbcon
into your SQL string itself, here:
这个错误不是mysql和mysqli混用引起的。它是由插入$dbcon
到您的 SQL 字符串本身引起的,这里:
$results = mysql_query("SELECT * FROM items WHERE item_id = 1,$dbcon");
// ^ here
The connection should be passed as the firstargument with the query as the second. You're inserting it into the SQL string and passing it as a single argument, which means PHP tries to convert the object to a string.
连接应该作为第一个参数传递,查询作为第二个参数。您将其插入 SQL 字符串并将其作为单个参数传递,这意味着 PHP 会尝试将对象转换为字符串。
Change to:
改成:
$results = mysqli_query($dbcon, "SELECT * FROM items WHERE item_id = 1");
As others point out, you need to change all mysql_
references to mysqli_
but this is your secondary problem. The immediate problem is the string issue and once that's fixed, you will encounter the mixing mysql problem.
正如其他人指出的那样,您需要更改所有mysql_
引用,mysqli_
但这是您的次要问题。直接的问题是字符串问题,一旦修复,您将遇到混合 mysql 问题。