数据库没有返回结果
Database Returning no results
我最近从完美运行的 CodeIgniter v2.2.0 升级到 CodeIgniter v3.0.1 以获得更好的会话处理。这样做时,所有对数据库的调用,无论使用哪个 $database['dbdriver']
return 都没有。我也试过像这样将 $config
数组直接输入数据库加载器:$this->load->database($config);
但这也不起作用。我试过使用 PDO 和 MySQLi 驱动程序,两者 return 是一样的:
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`
table绝对不是空的,它包含超过300万条登录记录,我什至尝试将限制设置为1以防records/data的数量太多.
我是否遗漏了我的配置中应该包含的任何内容?以下是我的配置(请记住,我已 CI 设置为在同一 CI 安装中处理多个应用程序)
config/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');
controllers/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;
}
}
models/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;
}
}
感谢任何帮助,我只是不知道我在这里缺少什么。
不确定为什么 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;
}
我最近从完美运行的 CodeIgniter v2.2.0 升级到 CodeIgniter v3.0.1 以获得更好的会话处理。这样做时,所有对数据库的调用,无论使用哪个 $database['dbdriver']
return 都没有。我也试过像这样将 $config
数组直接输入数据库加载器:$this->load->database($config);
但这也不起作用。我试过使用 PDO 和 MySQLi 驱动程序,两者 return 是一样的:
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`
table绝对不是空的,它包含超过300万条登录记录,我什至尝试将限制设置为1以防records/data的数量太多.
我是否遗漏了我的配置中应该包含的任何内容?以下是我的配置(请记住,我已 CI 设置为在同一 CI 安装中处理多个应用程序)
config/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');
controllers/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;
}
}
models/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;
}
}
感谢任何帮助,我只是不知道我在这里缺少什么。
不确定为什么 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;
}