代码点火器中的查询一直返回“1”

时间:2009-11-18 04:44:01

标签: php codeigniter

我是一个代码点火器菜鸟。我有一个如下控制器:

<?php
class Welcome extends Controller {
    function Welcome()
    {
        parent::Controller();   
        $this->load->model('usermodel');
    }

    function index()
    {
        $res = $this->db->get('users');
        die( print_r($res) );
    }
}

我的数据库中有一个'用户'表,我已经检查了三次数据库凭据。但是,打印方法只保持打印“1”。任何人都知道为什么这个简单的查询不起作用?

另外值得注意的是print_r打印出一个对象,我确实在我的database.php文件中设置了active_record = true。如果它有帮助,这是

的输出
print_r($this->db);

打印

CI_DB_mysql_driver Object ( [dbdriver] => mysql [_escape_char] => ` [delete_hack] => 1 [_count_string] => SELECT COUNT(*) AS [_random_keyword] => RAND() [ar_select] => Array ( ) [ar_distinct] => [ar_from] => Array ( ) [ar_join] => Array ( ) [ar_where] => Array ( ) [ar_like] => Array ( ) [ar_groupby] => Array ( ) [ar_having] => Array ( ) [ar_limit] => [ar_offset] => [ar_order] => [ar_orderby] => Array ( ) [ar_set] => Array ( ) [ar_wherein] => Array ( ) [ar_aliased_tables] => Array ( ) [ar_store_array] => Array ( ) [ar_caching] => [ar_cache_exists] => Array ( ) [ar_cache_select] => Array ( ) [ar_cache_from] => Array ( ) [ar_cache_join] => Array ( ) [ar_cache_where] => Array ( ) [ar_cache_like] => Array ( ) [ar_cache_groupby] => Array ( ) [ar_cache_having] => Array ( ) [ar_cache_orderby] => Array ( ) [ar_cache_set] => Array ( ) [username] => root [password] => my_password [hostname] => localhost [database] => bhr_development [dbprefix] => [char_set] => utf8 [dbcollat] => utf8_general_ci [autoinit] => 1 [swap_pre] => [port] => [pconnect] => 1 [conn_id] => [result_id] => [db_debug] => [benchmark] => 0 [query_count] => 0 [bind_marker] => ? [save_queries] => 1 [queries] => Array ( [0] => SELECT * FROM (`users`) ) [query_times] => Array ( [0] => 0 ) [data_cache] => Array ( ) [trans_enabled] => 1 [trans_strict] => 1 [_trans_depth] => 0 [_trans_status] => [cache_on] => [cachedir] => [cache_autodel] => [CACHE] => [_protect_identifiers] => 1 [_reserved_identifiers] => Array ( [0] => * ) [stmt_id] => [curs_id] => [limit_used] => ) 1

更新 我试过像:

$query = $this->db->get("users");
        foreach($query->result() as $row){
            echo "hello";
        }
        die();
没有运气。仍然得到:

Fatal error: Call to a member function result() on a non-object in ...welcome.php...

5 个答案:

答案 0 :(得分:2)

$res是一个结果对象,而不是成功查询产生的字符串。您是否尝试在$res->num_rows()循环中调用$res->result()或迭代foreach并打印出列的值(例如echo $res->col_name)?

另外(我想我应该先问过这个),那个表中是否有任何行?

答案 1 :(得分:0)

您的查询未返回1,这是来自die语句。除非传递第二个参数,否则print_r函数不会返回值。发生的事情是print_r函数评估为true并由die函数打印。

如果要查看查询结果,则必须使用http://codeigniter.com/user_guide/database/results.html中所述的result或result_array函数。没有捷径。

答案 2 :(得分:0)

正确用法是:

$res = $this->db->get('users');

foreach ($res->result() as $row)
{
    echo $row->title;
}

在迭代它们之前,不会加载实际结果数据行。

答案 3 :(得分:0)

为使查询正常工作,我希望您首先需要初始化数据库类:

$this->load->database();
$res = $this->db->get('users');

答案 4 :(得分:0)

我确实有类似的问题。我的代码输出一个大的PDF,总执行时间超过60秒。我把数据库连接丢失了。

尝试$this->db->reconnect();并且不要忘记在您的database.php中有$db['default']['db_debug'] = TRUE;