codeigniter选择查询问题

时间:2016-03-31 12:59:54

标签: php mysql codeigniter

在我的codeigniter应用程序中尝试从数据库中检索数据。为此,我使用以下代码

database.php中:

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

$db['default']['hostname'] = "localhost";
$db['default']['username'] = "*****";
$db['default']['password'] = "****";

$db['default']['database'] = "mydbname";
$db['default']['dbdriver'] = 'mysqli';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = FALSE;
$db['default']['db_debug'] = FALSE;
$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;

在Model.php中:

    $sql = "SELECT * from users";

    $query = $this->db->query($sql);
    print_r($query);

但它会像

一样返回
CI_DB_mysqli_result Object ( [conn_id] => [result_id] => [result_array] => Array ( ) [result_object] => Array ( ) [custom_result_object] => Array ( ) [current_row] => 0 [num_rows] => [row_data] => ) array(0) { }

空结果。我怎样才能从中获得结果。

1 个答案:

答案 0 :(得分:2)

这样做:

<?php
$sql = "SELECT * from users";
$query = $this->db->query($sql);
if($query->num_rows() > 0) {
    print_r($query->result_array());
} else {
    $d = array();
    print_r($d);
}
?>