用户
 找回密码
 入住 CI 中国社区
搜索
查看: 3452|回复: 1
收起左侧

2.1.3 pdo num_rows 连续查询结果输出错误。

[复制链接]
发表于 2012-12-8 02:16:43 | 显示全部楼层 |阅读模式
接触CI时间不长,才十来天,今天尝试创建自己的模型,用的Sqlite数据库,使用PDO连接方式,结果 使用num_rows()在模型内其它函数查询后会得到行数加1,更奇怪的是连续进行赋值也会出现问题,程序片断如下:
PHP复制代码
 
function create(){
      $table_names = array('system_log','system_log','system_log','system_tables');
      foreach ($table_names as $key => $table_name) {
     $sql = "SELECT name FROM sqlite_master WHERE type ='table' AND name = '".$table_name."'";
             $sqlite_query = $this->db->query($sql);
             $row = $sqlite_query->row();
             $num_rows = $sqlite_query->num_rows();
             $num_rows1 = $sqlite_query->num_rows();
             $num_rows2 = $sqlite_query->num_rows();
             echo '<br>**********<br>';
             echo $sql.'<br>query('.$table_name.')---re num_rows-1:'.$num_rows.'--2:'.$num_rows1.'---3:'.$num_rows2.'----4:'.$sqlite_query->num_rows().'-------------------<br>';
             if($sqlite_query->num_rows() == 0){
                  //数据表创建。                
                 $this->auto_create($table_name);
                 echo $key.' -Create- '.$table_name.'==Yes <br>';
             }else{
                echo $key.' -Create- '.$row->name.'==Not <br>';
             }
         }
  }
 
复制代码

执行结果如下图:(传不上图)
HTML复制代码
<br>**********<br>SELECT name FROM sqlite_master WHERE type ='table' AND name = 'system_log'<br>query(system_log)---re num_rows-1:0--2:0---3:0----4:0-------------------<br>-auto_create: system_log---------------<br>0 -Create- system_log==Yes <br><br>**********<br>SELECT name FROM sqlite_master WHERE type ='table' AND name = 'system_log'<br>query(system_log)---re num_rows-1:1--2:1---3:1----4:1-------------------<br>1 -Create- system_log==Not <br><br>**********<br>SELECT name FROM sqlite_master WHERE type ='table' AND name = 'system_log'<br>query(system_log)---re num_rows-1:1--2:1---3:1----4:1-------------------<br>2 -Create- system_log==Not <br><br>**********<br>SELECT name FROM sqlite_master WHERE type ='table' AND name = 'system_tables'<br>query(system_tables)---re num_rows-1:1--2:1---3:1----4:1-------------------<br><div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">
<h4>遇到一个PHP错误</h4>
<p>Severity: 注意</p>
<p>Message:  Trying to get property of non-object</p>
<p>Filename: system_db/system_basic.php</p>
<p>Line Number: 102</p>

</div>3 -Create- ==Not <br>
复制代码


num_rows 大于0后就一起保持,不能得出准确行数,后来我研究了下CI 的PDO驱动,将
PHP复制代码
class CI_DB_pdo_result extends CI_DB_result {[/font]
[font=微软雅黑] public $num_rows;[/font]
[font=微软雅黑] /**
  * Number of rows in the result set
  *
  * @return int
  */

 public function num_rows()
 {
  if (is_int($this->num_rows))
  {
   return $this->num_rows;
  }
  elseif (($this->num_rows = $this->result_id->rowCount()) > 0)
  {
   //return $this->num_rows;  注释这行后正常咯。
  }[/font]
[font=微软雅黑]  $this->num_rows = count($this->result_id->fetchAll());
  $this->result_id->execute();
  return $this->num_rows;
 }
 
复制代码

return $this->num_rows;  注释这行后正常咯。
但我对CI 核心根本不清楚,想问的是,这行注释了会造什么后果喃?
非常感谢论坛的高手达人可以帮忙解决下这个问题!
发表于 2013-6-15 14:56:49 | 显示全部楼层
碰到一样的问题了。唉。也不知道要怎么弄

本版积分规则