我最近从完美的CodeIgniter v2.2.0升级到CodeIgniter v3.0.1,以便更好地进行会话处理。在这样做时,无论使用哪个$database['dbdriver']
,所有对数据库的调用都不会返回任何内容。我也尝试将$config
数组直接输入到数据库加载器中,如下所示:$this->load->database($config);
但这也不起作用。我尝试过使用PDO和MySQLi驱动程序,两者都返回相同的东西:
库MySQLi:
string 'SELECT *
FROM `ns_users`' (length=24)
object(CI_DB_mysqli_result)[26]
public 'conn_id' =>
object(mysqli)[24]
public 'affected_rows' => null
public 'client_info' => null
public 'client_version' => null
public 'connect_errno' => null
public 'connect_error' => null
public 'errno' => null
public 'error' => null
public 'error_list' => null
public 'field_count' => null
public 'host_info' => null
public 'info' => null
public 'insert_id' => null
public 'server_info' => null
public 'server_version' => null
public 'stat' => null
public 'sqlstate' => null
public 'protocol_version' => null
public 'thread_id' => null
public 'warning_count' => null
public 'result_id' =>
object(mysqli_result)[25]
public 'current_field' => null
public 'field_count' => null
public 'lengths' => null
public 'num_rows' => null
public 'type' => null
public 'result_array' =>
array (size=0)
empty
public 'result_object' =>
array (size=0)
empty
public 'custom_result_object' =>
array (size=0)
empty
public 'current_row' => int 0
public 'num_rows' => null
public 'row_data' => null
PDO:
string 'SELECT *
FROM `ns_users`' (length=24)
object(CI_DB_pdo_result)[26]
public 'conn_id' =>
object(PDO)[23]
public 'result_id' =>
object(PDOStatement)[25]
public 'queryString' => string 'SELECT *
FROM `ns_users`' (length=24)
public 'result_array' =>
array (size=0)
empty
public 'result_object' =>
array (size=0)
empty
public 'custom_result_object' =>
array (size=0)
empty
public 'current_row' => int 0
public 'num_rows' => null
public 'row_data' => null
我正在尝试运行的简单查询是,因此我可以看到数据是从数据库中提取的:
SELECT * FROM `ns_users`
该表绝对不是空的,它包含超过300万个登录记录,我甚至试图在记录/数据量过多的情况下将其限制为1。
我是否遗漏了应该在我的配置中的任何内容?以下是我的配置(请记住,我已设置CI以在同一CI安装上处理多个应用程序)
配置/ database.php中
<?php
$db['users'] = array(
'dsn' => 'mysql:host=localhost;dbname=apps_control',
'hostname' => 'localhost',
'username' => '***********',
'password' => '***********',
'database' => 'apps_control',
'dbdriver' => 'pdo',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => TRUE,
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => TRUE,
'failover' => array(),
'save_queries' => TRUE
);
define('USERS_DB_GROUP', 'users');
控制器/ test.php的
<?php
class Test extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function index(){
$this->load->model('Users');
var_dump($this->Users->get_all_users());
exit;
}
}
模型/ users.php
<?php
class Users extends CI_Model {
private $table_name;
private $udb;
public function __construct() {
parent::__construct();
}
public function init($config = null){
if(!empty($config) && is_array($config))
$this->udb = $this->load->database($config, TRUE);
else
$this->udb = $this->load->database(USERS_DB_GROUP, TRUE);
if(!$this->table_name)
$this->table_name = $this->udb->dbprefix('ns_users');
}
public function get_all_users(){
$this->init();
$result = $this->udb->get($this->table_name);
var_dump($this->udb->last_query());
var_dump($result);exit;
if($result && $result->num_rows > 0){
return $result->result_array();
}
return false;
}
}
感谢任何帮助,我只是在失去我在这里所缺少的东西。
答案 0 :(得分:1)
不确定为什么var_dump($result)
会显示您的num_rows
0
尝试使用num_rows()(作为函数)比较您的结果
if($result && $result->num_rows() > 0){
但我会写这样的函数
public function get_all_users()
{
$this->init();
$result = $this->udb->get($this->table_name)->result_array();
if($result)//check if our array empty or not.
{
return $result;
}
return false;
}