2014-12-28 92 views

回答

2

的问题

不能呼应$result,因为它不是一个字符串。 It's a mysqli_result object. That's what mysqli_query returns if your query works

在PHP中,您不能使用echo对象,除非它有一个__toString magic methodThe mysqli_result object has no such method,所以它不能是echo编。因此,错误。

解决方案

什么你做的是循环使用过的one of the fetch_* methods or functions结果,如mysqli_fetch_assoc()。该文档包含示例,并且有大量关于使用MySQLi的优秀教程。

下面是一个使用mysqli_fetch_assoc()一个例子:

<?php  
$host="localhost"; // Host name 
$username=""; // Mysql username 
$password=""; // Mysql password 
$db_name="tianen"; // Database name 
$tbl_name="product"; 

$con = mysqli_connect("127.0.0.1","root","","tianen"); 

$result=mysqli_query($con,"SELECT * FROM product"); 

if ($result) { 
    /* fetch associative array */ 
    while ($row = mysqli_fetch_assoc($result)) { 
     // do things with $row, an associative array with keys matching your table's column names 
    } 
?> 
1

mysqli_query将返回一个对象,你就必须从提取行。一旦你有了行,你就可以访问每一行的字段。

我做到这一点的方法是通过mysqli的结果循环(我不知道是否有另一种方式):

// Create an empty array to store rows 
$rows = []; 

// The fetch_assoc function will automatically generate an iterator to return false when there are no more rows to fetch. 
// Each result will be a row in the form of an associative array 
while ($row = mysqli_fetch_assoc($con, $result)) { 
    array_push($rows, $row); 
} 

还有另一种方式来使用的mysqli为对象,我觉得这更容易,因为1.函数名称较短,2.您不必传递$ con参数。相反,你会说$result = $con->query("SELECT ...");

相关问题