Obtain the field value of the object in the record queried by the Codeigniter database. this is a piece of program models layer functioncheckUser () {& nbsp; $ username = $ this-& gt; input-& gt; post ('Username'); & nbsp get the field value of the object in the record queried by the Codeigniter database
This is a piece of code in codeigniter.
Models layer
Function checkUser ()
{
$ Username = $ this-> input-> post ('Username ');
$ Password = $ this-> input-> post ('password ');
$ This-> db-> from ('user ');
$ This-> db-> where ('username', $ username );
$ This-> db-> where ('password', $ password );
$ Query = $ this-> db-> get ();
Return $ query-> result ();
}
Controller layer:
Public function User_Login ()
{
$ This-> load-> model ('user', '', TRUE );
$ Error ['info'] = "incorrect user name or password! ";
$ Data ['query'] = $ this-> User-> checkUser ();
$ Info = $ this-> User-> checkUser ();
// Print the user information obtained here
Print_r ($ info );
If (! Empty ($ data ['query'])
{
$ This-> load-> view ('user/User_Index ');
}
Else
{
$ This-> load-> view ('index', $ error );
}
}
Here is a bit of a tangle of knowledge points!
The printed information is as follows:
Array ([0] => stdClass Object ([id] => 1 [createdate] => 14:32:18 [flag] => 1 [password] => 123 [username] => user1 [email] => ))
How can I obtain the id of the first object in this array ???
------ Solution --------------------
PHP code
// Modelfunction checkUser () {// omit other code // my personal understanding, only one object will be returned here, it is better to directly return a content $ res_data = $ query-> result_array (); // or $ res_data = $ query-> result (); // if (count ($ res_data) = 1) {$ res_data = reset ($ res_data);} return $ res_data ;} // controllerpublic function User_Login () {$ this-> load-> model ('user', '', TRUE ); $ error ['info'] = "incorrect user name or password! "; $ Data ['query'] = $ this-> User-> checkUser (); $ info = $ this-> User-> checkUser (); // Print the user information print_r ($ info) obtained here; // $ info-> id if (! Empty ($ info ['id']) {$ this-> load-> view ('user/User_Index ');} else {$ this-> load-> view ('index', $ error );}}