CodeIgniter来自Mysql DB查询的结果集

时间:2016-10-10 16:23:42

标签: mysql codeigniter

我正在将AWS Ubuntu 12 / PHP 4.2 / CI 1.3.1上的现有CI项目更新为运行Ubuntu 16.0.4的新AWS实例LTS / Apache 2.4.18 / PHP 7.0.8 / MySQL 5.7.15 / CI 3.1.0。

我可以使用database.php中的设置连接并从mysql命令行中选择数据我写了一个非CI测试PHP页面,它也返回正确的结果。模型中的相同查询返回0结果。

我一直在调整试图让它工作的设置。这是 database.php 的当前快照。

$active_group = 'default';
$query_builder = TRUE;
//  'db_debug' => (ENVIRONMENT !== 'production'),

$db['default'] = array(
    'dsn'   => '',
    'hostname' => '127.0.0.1',
    'username' => '****',
    'password' => '****',
    'database' => '****',
    'dbdriver' => 'mysqli',
    'dbprefix' => '',
    'pconnect' => TRUE,
    'db_debug' => FALSE,
    'cache_on' => FALSE,
    'cachedir' => '',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    'swap_pre' => '',
    'encrypt' => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE,
    'failover' => array(),
    'save_queries' => TRUE
);

以下是该模型的适用部分:

class User_model extends CI_Model
{
    function __construct()
    {
        parent::__construct();
        log_message("info","User_model class instantiated");
    }

... section omitted ...

    function login($username, $password)
    {
        $this->load->library('encrypt');
        // Get the hash of the entered password to lookup in the database
        //$password_hash = $this->encrypt->sha1($password);  (Removed 10/8/2016 ES SHA1 deprecated)

        $newpassword_hash = $this->encrypt->encode($password);

        $this->db->where('user_name', $username );
        $this->db->or_where('email', $username);

        $query = $this->db->get('users', 1);

        if($query->num_rows == 0){
            log_message("info","User not found");
            return false;
        }
        else
        {
... remainder omitted ...

的var_dump($这 - > DB-> last_query());

string(107) "SELECT * FROM `users` WHERE `user_name` = 'xxxxxx@gmail.com' OR `email` = 'xxxxxx@gmail.com' LIMIT 1" 

的var_dump($查询);

object(CI_DB_mysqli_result)#23 (8) { ["conn_id"]=> object(mysqli)#15 (19) { ["affected_rows"]=> int(1) ["client_info"]=> string(79) "mysqlnd 5.0.12-dev - 20150407 - $Id: 241ae00989d1995ffcbbf63d579943635faf9972 $" ["client_version"]=> int(50012) ["connect_errno"]=> int(0) ["connect_error"]=> NULL ["errno"]=> int(0) ["error"]=> string(0) "" ["error_list"]=> array(0) { } ["field_count"]=> int(60) ["host_info"]=> string(20) "127.0.0.1 via TCP/IP" ["info"]=> NULL ["insert_id"]=> int(0) ["server_info"]=> string(23) "5.7.15-0ubuntu0.16.04.1" ["server_version"]=> int(50715) ["stat"]=> string(138) "Uptime: 229367 Threads: 7 Questions: 1083 Slow queries: 0 Opens: 440 Flush tables: 1 Open tables: 241 Queries per second avg: 0.004" ["sqlstate"]=> string(5) "00000" ["protocol_version"]=> int(10) ["thread_id"]=> int(223) ["warning_count"]=> int(0) } ["result_id"]=> object(mysqli_result)#22 (5) { ["current_field"]=> int(0) ["field_count"]=> int(60) ["lengths"]=> NULL ["num_rows"]=> int(1) ["type"]=> int(0) } ["result_array"]=> array(0) { } ["result_object"]=> array(0) { } ["custom_result_object"]=> array(0) { } ["current_row"]=> int(0) ["num_rows"]=> NULL ["row_data"]=> NULL } 

1 个答案:

答案 0 :(得分:0)

如果您使用$query = $this->db->get('users', 1);此格式在CI中进行查询 那么行数将通过以下方式实现:

$this->db->where('user_name', $username );
$this->db->or_where('email', $username);
$query = $this->db->get('users')->result_array();

        if(count($query) == 0){
            log_message("info","User not found");
            return false;
        }
       else{
            //do what you want
        }

否则你可以使用如下:

 $query = $this->db->query('your sql here');

            if($query->num_rows == 0){
                log_message("info","User not found");
                return false;
            }
            else{
               //do what you want
            }

希望这可以解决您的问题。:)