PHP变量不是mysqli对象
问题描述:
I have the next code (compilated in NetBeans)
$query = 'INSERT INTO grupos(nombre, descripcion)'
. ' VALUES ("' . utf8_decode($_POST['name']) . '", "' . utf8_decode($_POST['desc']) . '")';
$result = $con->query($query) or exit("Error: " . $con->errno . ": " . $con->error);
if ($result->affected_rows > 0) {
Why I got this: "Trying to get property of non-object " if when I made a echo $result; display a '1'?
我有下一个代码(在NetBeans中编译) p>
$ query ='INSERT INTO grupos(nombre,descripcion)'
。 'VALUES(“''。utf8_decode($ _ POST ['name'])。'”,“'。utf8_decode($ _ POST ['desc'])。'”)';
$ result = $ con> query ($ query)或退出(“错误:”。$ con> errno。“:”。$ con>错误);
if($ result-> affected_rows> 0){
code> pre>
为什么我得到这个:“当我发出echo $结果时,”试图获得非对象的属性“
if; 显示'1'? p>
div>
答
From the documentation of mysqli::query()
:
For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE.
Since your query is INSERT
, not SELECT
, it doesn't return a mysqli_result
, it just returns TRUE
. You should use $con->affected_rows
instead of $result->affected_rows
.