Welcome Guest, Not a member yet? Register   Sign In
Connecting to multiple databases ?
#1

[eluser]spaquet[/eluser]
I read many posts on this forum but for now it does not seem to be working at all...

My application uses an ERP as backend and offer to some users a easy way to get data from it and track some of their activity on the side of this ERP, reason why there is a second database (to manange CI_Session, user activity and some requests that are not part of the ERP).

What I've done:

I added a second database in database.php (sugarcrm) and have the following code for my user controller :

Code:
Class User_model extends CI_Model {

    private $db_erp;

    function __construct() {
        // Call the Model constructor
        parent::__construct();

        $this->db_erp = $this->load->database('sugarcrm', TRUE);
    }

    public function validates_user_from_erp($matricule, $password) {
        $sql = "SELECT * FROM users JOIN users_cstm ON users.id=users_cstm.id_c ";
        $sql += "WHERE users_cstm.matricule_c=" + $matricule + " AND ";
        $sql += "users_cstm.password_c=md5('" + $password + "');";

        $query = $this->db_erp->query($sql);
        
        if ($query->num_rows == 1) {
            return true;
        }
        
        return false;
    }

I made sure the database user connect to the database and that the query is valid (I tested in on the database).

Now when calling alidates_user_from_erp from my login controller

Code:
public function validate_credentials() {
        $this->output->enable_profiler(TRUE);

        $this->form_validation->set_rules('username', 'Utilisateur', 'required');
        $this->form_validation->set_rules('password', 'Mot de passe', 'required');

        if ($this->form_validation->run() == FALSE) {
            $this->index();
        } else {
            $this->load->model('User_model');
            
            //Check if user is active and if his/her credentials are valid
            $username = $this->input->post('username'); // aka matricule
            $password = $this->input->post('password');
            
            if( $this->User_model->validates_user_from_erp($username, $password) ) {
                
                redirect('dashboard');
                    
            } else {
                $this->index();
            }
        }
    }

I have the following error message :
Quote:Trying to get property of non-object
refering to the line
Quote:$query->num_rows == 1
of my User model.

It looks like the $query object is not a database object as expected and thus I can not use database methods on it.

Any help is welcome as I have the feeling I've done what I read in CI User Guide and several posts from this forum.
#2

[eluser]InsiteFX[/eluser]
Maybe because your calling num_rows wrong!
Code:
// wrong!
if ($query->num_rows == 1)

// should be:
if ($query->num_rows() == 1)

InsiteFX
#3

[eluser]spaquet[/eluser]
Now I have the following message:
Quote:PHP Fatal error: Call to a member function num_rows() on a non-object in user_model.php on line 24
#4

[eluser]InsiteFX[/eluser]
Well non-object meaqns it is not seeing the database!

I would verify that the database is being set.
Code:
function __construct() {
        // Call the Model constructor
        parent::__construct();

        $this->db_erp = $this->load->database('sugarcrm', TRUE);

        // test to see if the database is setting!
        var_dump($this->db_erp);
    }

InsiteFX
#5

[eluser]spaquet[/eluser]
Looks like it is connected, here is the dump:
Quote:object(CI_DB_mysqli_driver)#22 (70) { ["dbdriver"]=> string(6) "mysqli" ["_escape_char"]=> string(1) "`" ["_like_escape_str"]=> string(0) "" ["_like_escape_chr"]=> string(0) "" ["_count_string"]=> string(19) "SELECT COUNT(*) AS " ["_random_keyword"]=> string(7) " RAND()" ["delete_hack"]=> bool(true) ["ar_select"]=> array(0) { } ["ar_distinct"]=> bool(false) ["ar_from"]=> array(0) { } ["ar_join"]=> array(0) { } ["ar_where"]=> array(0) { } ["ar_like"]=> array(0) { } ["ar_groupby"]=> array(0) { } ["ar_having"]=> array(0) { } ["ar_keys"]=> array(0) { } ["ar_limit"]=> bool(false) ["ar_offset"]=> bool(false) ["ar_order"]=> bool(false) ["ar_orderby"]=> array(0) { } ["ar_set"]=> array(0) { } ["ar_wherein"]=> array(0) { } ["ar_aliased_tables"]=> array(0) { } ["ar_store_array"]=> array(0) { } ["ar_caching"]=> bool(false) ["ar_cache_exists"]=> array(0) { } ["ar_cache_select"]=> array(0) { } ["ar_cache_from"]=> array(0) { } ["ar_cache_join"]=> array(0) { } ["ar_cache_where"]=> array(0) { } ["ar_cache_like"]=> array(0) { } ["ar_cache_groupby"]=> array(0) { } ["ar_cache_having"]=> array(0) { } ["ar_cache_orderby"]=> array(0) { } ["ar_cache_set"]=> array(0) { } ["username"]=> string(10) "UUUUU" ["password"]=> string(11) "XXXXXXXX" ["hostname"]=> string(11) "10.10.11.42" ["database"]=> string(8) "sugarcrm" ["dbprefix"]=> string(0) "" ["char_set"]=> string(4) "utf8" ["dbcollat"]=> string(15) "utf8_general_ci" ["autoinit"]=> bool(true) ["swap_pre"]=> string(0) "" ["port"]=> string(0) "" ["pconnect"]=> bool(true) ["conn_id"]=> object(mysqli)#23 (17) { ["affected_rows"]=> int(0) ["client_info"]=> string(48) "mysqlnd 5.0.7-dev - 091210 - $Revision: 304625 $" ["client_version"]=> int(50007) ["connect_errno"]=> int(0) ["connect_error"]=> NULL ["errno"]=> int(0) ["error"]=> string(0) "" ["field_count"]=> int(0) ["host_info"]=> string(22) "10.10.11.42 via TCP/IP" ["info"]=> NULL ["insert_id"]=> int(0) ["server_info"]=> string(19) "5.1.41-3ubuntu12.10" ["server_version"]=> int(50141) ["sqlstate"]=> string(5) "00000" ["protocol_version"]=> int(10) ["thread_id"]=> int(210) ["warning_count"]=> int(0) } ["result_id"]=> bool(false) ["db_debug"]=> bool(false) ["benchmark"]=> int(0) ["query_count"]=> int(0) ["bind_marker"]=> string(1) "?" ["save_queries"]=> bool(true) ["queries"]=> array(0) { } ["query_times"]=> array(0) { } ["data_cache"]=> array(0) { } ["trans_enabled"]=> bool(true) ["trans_strict"]=> bool(true) ["_trans_depth"]=> int(0) ["_trans_status"]=> bool(true) ["cache_on"]=> bool(false) ["cachedir"]=> string(0) "" ["cache_autodel"]=> bool(false) ["CACHE"]=> NULL ["_protect_identifiers"]=> bool(true) ["_reserved_identifiers"]=> array(1) { [0]=> string(1) "*" } ["stmt_id"]=> NULL ["curs_id"]=> NULL ["limit_used"]=> NULL ["stricton"]=> bool(false) }

(Password and user values have been changed ;-)
#6

[eluser]InsiteFX[/eluser]
Your think you sql is wrong!

InsiteFX
#7

[eluser]spaquet[/eluser]
Found the error ! String concatenation in PHP is . and not + (... ;-)

and by the way I can now use
Code:
$query->num_rows
, it's just working great !




Theme © iAndrew 2016 - Forum software by © MyBB