如何在库CodeIgniter中获取用户ID

时间:2017-05-28 10:12:49

标签: php mysql codeigniter

如何在库中获取用户ID

用户表

| ID |用户名| | --------------- | | 1 |亚历克斯| | 2 | Johey | | 3 |青少狮| | 4 |帕卡特|

存储表

| ID | userID | store_picture |
|-----------------------------|
| 1  | Alex   | img1.jpg      |
| 2  | Johey  | img3.jpg      |
| 3  | Leos   | img2.jpg      |
| 4  | Pacat  | img5.jpg      |

库/ Store.php

<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 

class Store 
{

    var $info=array();

    public function __construct() 
    {
        $CI =& get_instance();
        $site = $CI->db->select("userID,store_background")
        ->where("userID", 3)
        ->get("store_info");

        if($site->num_rows() == 0) {
            $CI->template->error(
                "You are missing the site settings database row."
            );
        } else {
            $this->info = $site->row();
        }
    }

}

?>

由于

2 个答案:

答案 0 :(得分:0)

改为使用get_instance():

//Model name : Store_info_model.php
function get_userID( $id ) {
    $this->db->select( 'userID, store_background' );
    $this->db->where( 'userID', $id );
    return $this->db->get( 'store_info' );
}

<?php
    if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 
    class Store { 
        public $info=array(); 
        public function __construct() { 
            $CI =& get_instance(); 
            $CI->load->model( 'Store_info_model' );
            $site = $CI->Store_info_model->get_userID( 3 );
            if($site->num_rows() == 0) { 
                //Do something when error happen
            } 
            else $this->info = $site->row();  
        } 
    } 
?> 

答案 1 :(得分:0)

尝试在库中加载数据库或在配置中添加自动加载

在你的库

中添加它
$this->load->database();