Model Returns Null on FindAll - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28) +--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30) +--- Thread: Model Returns Null on FindAll (/showthread.php?tid=76442) Pages:
1
2
|
Model Returns Null on FindAll - remesses_thegreat - 05-12-2020 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) { } RE: Model Returns Null on FindAll - remesses_thegreat - 05-13-2020 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 RE: Model Returns Null on FindAll - jreklund - 05-13-2020 Have you tried with a capital A? The real name of the function are findAll. RE: Model Returns Null on FindAll - vitnibel - 05-13-2020 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? RE: Model Returns Null on FindAll - remesses_thegreat - 05-14-2020 (05-13-2020, 01:03 PM)jreklund Wrote: Have you tried with a capital A? Good Day. Thanks for the response. Just tried it with a capital A now it returns Array ( ) with no curly brackets RE: Model Returns Null on FindAll - remesses_thegreat - 05-14-2020 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 RE: Model Returns Null on FindAll - jreklund - 05-14-2020 Can you try a var_dump($this->db); inside a function in your model and see if it's connected. RE: Model Returns Null on FindAll - remesses_thegreat - 05-14-2020 (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) { } } RE: Model Returns Null on FindAll - jreklund - 05-15-2020 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; RE: Model Returns Null on FindAll - remesses_thegreat - 05-15-2020 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? |