php - CodeIgniter Sqlite 不工作

标签 php sql codeigniter sqlite

每当我在我的模型中像这样查询我的数据库(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,网络服务器支持它。 有人知道我哪里出错了吗?

--------完整代码: 我在 models/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 中的 Controller :

<?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 的功劳。 答案很长,但我希望这对您有所帮助。

关于php - CodeIgniter Sqlite 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22941817/

相关文章:

php - $_FILES ['file' ] ['name' ] 在 codeigniter 中

php - 使用 PHP 将数据库数据编码为 JSON

php - Laravel 5.1 Eloquent orm 关系

具有多个where条件的MySQL连接查询

sql - 将数据从 .csv 复制到 Vertica 表

codeigniter - 如何让 form_validation 在 Codeigniter 中正常工作

php - 如何在 PHP 中获取 MySQL 表中刚刚插入的行?

PHP:当客户端在写入时断开连接时,socket_write 挂起

php - 在 mysql_fetch_array() 上循环运行时使用 PHP 的奇怪结果

php - MySQL查询优化——子查询+多重连接