mysql - 尝试对记录进行计数时,无法将mysqli_result类的对象转换为字符串

标签 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();}

    >

最佳答案

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

所以试试这个:

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

关于mysql - 尝试对记录进行计数时,无法将mysqli_result类的对象转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26986850/

相关文章:

mysql - 有没有一种简单的方法可以按照一个索引更新所有记录?

mysql - 从别人计算一列并将结果写入表中

php - 构建一个 mysqli 查询,按顺序和每页显示

java - 使用 CASE 表达式的 JOOQ 排序

php - 不使用 PHP 和 Ajax 使用 session 登录另一个页面

python - 为什么某些 mysql 连接在删除 + 插入后选择 mysql 数据库中的旧数据?

mysql - 我无法执行触发器中的两条语句

mysql - 子查询中的 DATE_ADD 会减慢执行速度

php - 我可以在同一语句中使用 LIKE 和 >= 吗?

php - 在新域/子域上复制 WordPress 站点