Model Returns Null on FindAll |
Good Day Everyone. Please urgently help with My Model. I want to get users from table users in db. Model returns Null/ array(0). Below is my code for the controller and model. Thanks in advance
UserModel PHP Code: <?php namespace App\Models; Controller PHP Code: public function get_users() Result array(0) { }
So The model can insert data to db without problem. Problem is retrieving it from the database. So any help would be appreciated. Is there any step i might be missing ? I want to be able to use functions like findall() first() but model returns null when i to dump result set
This is of course a very strange behavior, but it is a fact.
Calling a non-existent function in the model does not cause errors or exceptions, but simply returns NULL. Does anyone know why this is done?
So i tried this on my controller just testing if my connection is fine.
$db = \Config\Database::connect(); $query = $db->query('SELECT * FROM users'); $results = $query->getResult(); print_r($results); Result :Array ( [0] => stdClass Object ( [id] => 37 [user_id] => 9347985374985 [first_name] => Castro [last_name] => Nemaheni [phone] => 0984509343[email] => castro@company.co.za [office] => gauteng [created_at] => 2020/05/13 04:11:38 [updated_at] => [role] => homeowner [password] => $2y$10$fsaAR9VVYjbbAftCoJYLV./jcttCsTCLz1usp.4IpezJNlVUwITPS [activation] => 0 ) ) How do i resolve this. Because am trying to use model in this new way not like in ci3. Appreciate the help
Can you try a var_dump($this->db); inside a function in your model and see if it's connected.
(05-14-2020, 10:41 AM)jreklund Wrote: Can you try a var_dump($this->db); inside a function in your model and see if it's connected.Result for var_dump($this->db); object(CodeIgniter\Database\MySQLi\Connection)#49 (40) { ["DBDriver"]=> string(6) "MySQLi" ["deleteHack"]=> bool(true) ["escapeChar"]=> string(1) "`" ["mysqli"]=> NULL ["DSN":protected]=> string(0) "" ["port":protected]=> int(3306) ["hostname":protected]=> string(9) "localhost" ["username":protected]=> string(4) "root" ["password":protected]=> string(0) "" ["database":protected]=> string(9) "apples_db" ["subdriver":protected]=> NULL ["DBPrefix":protected]=> string(0) "" ["pConnect":protected]=> bool(false) ["DBDebug":protected]=> bool(false) ["cacheOn":protected]=> bool(false) ["cacheDir":protected]=> string(0) "" ["charset":protected]=> string(4) "utf8" ["DBCollat":protected]=> string(15) "utf8_general_ci" ["swapPre":protected]=> string(0) "" ["encrypt":protected]=> bool(false) ["compress":protected]=> bool(false) ["strictOn":protected]=> bool(false) ["failover":protected]=> array(0) { } ["lastQuery":protected]=> NULL ["connID"]=> bool(false) ["resultID"]=> bool(false) ["protectIdentifiers"]=> bool(true) ["reservedIdentifiers":protected]=> array(1) { [0]=> string(1) "*" } ["likeEscapeStr"]=> string(13) " ESCAPE '%s' " ["likeEscapeChar"]=> string(1) "!" ["dataCache"]=> array(0) { } ["connectTime":protected]=> NULL ["connectDuration":protected]=> NULL ["pretend":protected]=> bool(false) ["transEnabled"]=> bool(true) ["transStrict"]=> bool(true) ["transDepth":protected]=> int(0) ["transStatus":protected]=> bool(true) ["transFailure":protected]=> bool(false) ["aliasedTables":protected]=> array(0) { } }
That looks correct to me. I'm afraid I can't say why you get that result. I have successfully tested in the model itself and in the controller without any issue.
PHP Code: <?php namespace App\Models;
Okay no problem please advice here. Am currently working on a property system and have already started the project using crud forms and ajax to post data.
I am using Model like we did in ci3 by creating get functions function verify_user($email) { $db = \Config\Database::connect(); $query = $db->query("SELECT * FROM users WHERE email='$email'"); $result = $query->getResult(); return $result; } Would you advice me to continue on this route or i should find a fix to the get data issue? |
Welcome Guest, Not a member yet? Register Sign In |