Note that there are some explanatory texts on larger screens.

plurals
  1. POCodeIgniter database retrieval issue
    text
    copied!<p>This is my first attempt to code in CodeIgniter. The problem is it's not able to retrieve data from the database. The error it's throwing is:</p> <pre><code>A PHP Error was encountered Severity: Notice Message: Undefined property: Site::$db Filename: core/Model.php Line Number: 51 </code></pre> <p>I could understand what to change in that line of <code>core/model.php</code>.</p> <p>And this is the another error:</p> <pre><code> Fatal error: Call to a member function query() on a non-object in C:\wamp\www\CI_one\application\models\site_model.php on line 9 </code></pre> <h3>Controller</h3> <pre><code>&lt;?php class Site extends CI_Controller { function datta(){ $this-&gt;load-&gt;model('site_model'); $get = $this-&gt;site_model-&gt;getValues(); $data['get'] = $get; $this-&gt;load-&gt;view('data3',$data); } } </code></pre> <h3>Model</h3> <pre><code>&lt;?php class site_model extends CI_Model{ function index(){ echo "default index"; } function getValues(){ $query = $this-&gt;db-&gt;query("select * from nav_menu"); if ($query-&gt;num_rows() &gt; 0) { foreach ($query-&gt;result() as $row) { echo $row-&gt;c1; echo $row-&gt;m2; echo $row-&gt;d3; } } return array( "name" =&gt; "nameless", "form" =&gt; "formless", "age" =&gt; "ageless"); } } </code></pre> <p>I've set database parameters in <code>app/config/database.php</code>.</p> <p>How do I make this work?</p> <hr/> <p>I solved it myself. I wiped off and recoded as follows to get the result...</p> <h3>Controller</h3> <pre><code>&lt;?php class Site extends CI_Controller { function datta(){ $this-&gt;load-&gt;database(); $this-&gt;load-&gt;model('site_model'); $get = $this-&gt;site_model-&gt;getValues(); $data['get'] = $get; $this-&gt;load-&gt;view('data3',$data); } } </code></pre> <p>Model as</p> <pre><code>&lt;?php class site_model extends CI_Model{ function index(){ echo "default index"; } function getValues(){ $query = $this-&gt;db-&gt;query("select * from nav_menu"); if ($query-&gt;num_rows() &gt; 0) { foreach ($query-&gt;result() as $row) { echo $row-&gt;c1; echo $row-&gt;m2; echo $row-&gt;d3; } } return array( "name" =&gt; "nameless", "form" =&gt; "formless", "age" =&gt; "ageless"); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload