尝试计算记录时,无法将类mysqli_result的对象转换为字符串

时间:2014-11-18 04:54:06

标签: mysql

运行以下代码时出现此错误:

捕获致命错误:类mysqli_result的对象无法转换为字符串..

但是当我直接在mysql中运行查询时,它给出的值为12

任何人都可以帮忙告诉我我做错了什么吗?感谢

<?
 include 'clx.php'; 

 // Create connection
 $conn = mysqli_connect($server, $user_name, $pass_word,$database);

 //Check connection

 if ($conn->connect_error) {
 die("Connection failed: " . $conn->connect_error);
  } 

  // sending query to count records
  $table = 'USERS';
  $query1 = "SELECT COUNT(*) FROM {$table}";

  echo $query1;

  $result = MYSQLI_QUERY($conn,$query1);
  IF (!$result) {
            DIE("Query to show fields from table failed");
           }

   echo $result; //this is where I get the error apperently $result is an array or an object but
                   why?
                   and when I use the same query directly in mysql it gives me a value of 12 (the
                   number of records in the table)

   if ($result== 0){
    echo("No results found matching your query - $query1"); // bah, modify the "Not Found" error
          for your needs.
    exit();}

    >

1 个答案:

答案 0 :(得分:0)

因为MYSQLI_QUERY在失败时返回false并在成功时返回object。你不能直接打印对象。

所以试试这个:

if ($result)
{
   // Fetch one and one row
   while ($row=mysqli_fetch_row($result))
   {
        echo $row[0];
   }
}