【问题标题】:How do i get number of rows returned by a query in recordset variable? [duplicate]如何获取记录集变量中查询返回的行数? [复制]
【发布时间】:2014-02-24 02:34:26
【问题描述】:
$a=$this->db->where('username', $user);
echo $a->num_rows();

我试过了,但是出现这个错误

Fatal error: Call to undefined method CI_DB_mysql_driver::num_rows() in C:\xampp\htdocs\Testing\CI\application\models\mymodel.php on line 20

【问题讨论】:

  • 您需要先执行您的查询。看this

标签: php codeigniter


【解决方案1】:

你应该试试这个

$a=$this->db->where('username', $user)->get('table_name')->num_rows();
echo $a;

【讨论】:

    【解决方案2】:

    您尚未执行实际查询。如果要执行查询然后找出结果的数量,请使用 Yami 的答案。如果你想在得到实际结果之前知道结果的数量,你可以使用这个:

    $this->db->where('username', $user);
    echo $this->db->count_all_results('yourtable');
    

    当您需要根据查询返回的行数进行处理时,后者很有用,例如分页。

    【讨论】:

      【解决方案3】:
      $this->db->from('YOUR_TABLE);
      $this->db->where('username', $user);
      $result = $this->db->get();
      echo $result->num_rows();
      

      【讨论】:

      • 发生数据库错误错误号:1064 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 2 行的 'WHERE username = 'username]'' 附近使用正确的语法 SELECT * WHERE username = 'username]' 文件名:C:\xampp\htdocs \Testing\CI\system\database\DB_driver.php 行号:330
      • 您必须指定从中选择的表。 $this->db->from('yourtable');
      • 您没有在查询中定义 SELECT 部分。看看我编辑的帖子
      【解决方案4】:

      如果您正在查找查询使用返回的行数,

      query->num_rows()//返回受影响的行数。

      确保在 $this->db->get(); 之后执行此操作

      所以要执行的步骤顺序应该是

      $this->db->select('select condition'); 
      
      $this->db->where('where condition');
      
      $this->db->from('table name');
      
      $Query = $this->db->get();
      
      $Query->num_rows();  //Returns number of rows returned by the query.
      

      更多信息请参考CodeIgniter手册。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-07
        • 2013-09-22
        • 2011-05-28
        • 2020-12-18
        • 1970-01-01
        相关资源
        最近更新 更多