加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – CodeIgniter Sqlite无法正常工作

发布时间:2020-12-13 17:30:19 所属栏目:PHP教程 来源:网络整理
导读:每当我在我的模型中查询我的数据库(sqlite)时(我使用codeigniter,完整代码如下): $this-db-select('post');$query = $this-db-get('posts');return $query-result_array(); 我收到以下错误: Fatal error: Call to a member function rowCount() on a non-o
每当我在我的模型中查询我的数据库(sqlite)时(我使用codeigniter,完整代码如下):

$this->db->select('post');
$query = $this->db->get('posts');
return $query->result_array();

我收到以下错误:

Fatal error: Call to a member function rowCount() on a non-object in /codeigniter/system/database/drivers/pdo/pdo_result.php on line 42

当将查询更改为不存在的内容时,我会收到“正确”错误,例如:

A Database Error Occurred
Error Number: HY000
no such column: posst
SELECT posst FROM posts
Filename: /codeigniter/models/post.php
Line Number: 8

这让我相信数据库实际上正在运行,但是我有些遗漏.
我试过重新创建数据库.它实际上有1个表,1列,但我无法获得任何数据.我也尝试使用不同的“管理”程序创建它,但无济于事.我确定它是一个Sqlite 3 db,根据phpinfo网络服务器支持它.
有人在我犯错的地方有线索吗?

——–完整代码:
我的模型/ post.php中的帖子模型

<?php

class Post extends CI_Model{

    function get_posts(){

         $this->db->select('posst');
         $query = $this->db->get('posts');
         return $query->result_array();

        } 
}

我的控制器在controller / posts.php中:

<?php

class Posts extends CI_Controller{

    function index(){

        $this->load->model('post');
        $data['posts']=$this->post->get_posts();
        echo"<pre>";
        print_r($data['posts']);
        echo"</pre>";
    }

}

我在database.php中的数据库配置:

$active_group = 'default';
$active_record = TRUE;

$db['default']['hostname'] = 'sqlite:/home/******/******/www/wtp3/codeigniter/db/wtp35.sqlite';
$db['default']['username'] = '';
$db['default']['password'] = '';
$db['default']['database'] = '';
$db['default']['dbdriver'] = 'pdo';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;

解决方法

这个修复的功劳是与S.Stüvel,J.Bransen和S. Timmer一起.这是针对特定服务器的修复,因此YMMV.它虽然为我做了伎俩.

在pdo_driver.php中,第81行开始更改:

empty($this->database) OR $this->hostname .= ';dbname='.$this->database;

    $this->trans_enabled = FALSE;

    $this->_random_keyword = ' RND('.time().')'; // database specific random keyword
}

if(strpos($this->database,'sqlite') !== FALSE) {
        $this->hostname = $this->database;
        $this->_random_keyword = ' RANDOM()';
    }
    else {
        $this->hostname .= ";dbname=".$this->database;
        $this->_random_keyword = ' RND('.time().')'; // database specific random keyword
    }

    $this->trans_enabled = FALSE;
}

在第189行将整个函数_execute($sql)更改为

function _execute($sql)
{
    $sql = $this->_prep_query($sql);
    $result_id = $this->conn_id->query($sql);

    if (is_object($result_id))
    {
        $this->affect_rows = $result_id->rowCount();
    }
    else
    {
        $this->affect_rows = 0;
    }

    return $result_id;
}

然后在pdo_result.php中更改“:
在第29行改变

public $num_rows;

var $pdo_results = '';
var $pdo_index = 0;

在36号线上取代整个功能

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;
    }

    $this->num_rows = count($this->result_id->fetchAll());
    $this->result_id->execute();
    return $this->num_rows;
}

有:

function num_rows()
{
    if ( ! $this->pdo_results ) {
        $this->pdo_results = $this->result_id->fetchAll(PDO::FETCH_ASSOC);
    }
    return sizeof($this->pdo_results);

然后在第60行改变

function num_fields()
{
    return $this->result_id->columnCount();
}

至:

function num_fields()
{
    if ( is_array($this->pdo_results) ) {
        return sizeof($this->pdo_results[$this->pdo_index]);
    } else {
        return $this->result_id->columnCount();
    }
}

然后在第94行更改:

function field_data()
{
    $data = array();

    try
    {
        for($i = 0; $i < $this->num_fields(); $i++)
        {
            $data[] = $this->result_id->getColumnMeta($i);
        }

        return $data;
    }
    catch (Exception $e)
    {
        if ($this->db->db_debug)
        {
            return $this->db->display_error('db_unsuported_feature');
        }
        return FALSE;
    }
}

至:

function field_data()
{
    if ($this->db->db_debug)
    {
        return $this->db->display_error('db_unsuported_feature');
    }
    return FALSE;
}

然后第146行改变:

return FALSE;

$this->pdo_index = $n;

然后在第159行改变

function _fetch_assoc()
{
    return $this->result_id->fetch(PDO::FETCH_ASSOC);
}

function _fetch_assoc()
{
    if ( is_array($this->pdo_results) ) {
        $i = $this->pdo_index;
        $this->pdo_index++;
        if ( isset($this->pdo_results[$i]))
            return $this->pdo_results[$i];
        return null;
    }
    return $this->result_id->fetch(PDO::FETCH_ASSOC);
}

最后在第174行改变:

function _fetch_object()
{   
    return $this->result_id->fetchObject();

function _fetch_object()
{
    if ( is_array($this->pdo_results) ) {
        $i = $this->pdo_index;
        $this->pdo_index++;
        if ( isset($this->pdo_results[$i])) {
            $back = new stdClass();
            foreach ( $this->pdo_results[$i] as $key => $val ) {
                $back->$key = $val;
            }
            return $back;
        }
        return null;
    }
    return $this->result_id->fetch(PDO::FETCH_OBJ);
}

这对我有用.再一次,不是我的工作,信任是S.Stüvel,J.Bransen和S. Timmer.相当长的答案,但我希望这会有所帮助.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读