title: CodeIgniter: How do I fix "Trying to get property of non-object" and "Array to string conversion" errors and the reason for them to come?
body: <p>In 'new_payment' table and the 'new_reservation' table, each row (payment) has a res_id (PK in new_reservation and a reser, FK in new_payment, CASCADE, CASCADE). I have to fill 3 input boxes.</p>
  
  <p>Total Bill: [    ]</p>
  
  <p>Previously Paid: [    ]</p>
  
  <p>Due Amount: [    ]</p>
  
  <p>When user enters a room number and submit, controller calls 'search_by_room_number' model and the model then, </p>
  
  <ol>
  <li>Returns 'total' from new_reservation table</li>
  <li>Returns 'res_id' from new_reservation table into variable called $res_id</li>
  <li>With that $res_id, I look for previously 'paid_amount' in new_payment table</li>
  <li>...and 'due_amount' in new_payment table</li>
  </ol>
  
  <p><strong>But unexpectedly, following errors comes in:</strong></p>
  
  <pre><code>A PHP Error was encountered
  Severity: Notice
  Message: Array to string conversion
  Filename: models/payments_model.php
  Line Number: 16
  
  A PHP Error was encountered
  Severity: Notice
  Message: Trying to get property of non-object
  Filename: models/payments_model.php
  Line Number: 18
  
  A PHP Error was encountered
  Severity: Notice
  Message: Array to string conversion
  Filename: models/payments_model.php
  Line Number: 16
  
  A PHP Error was encountered
  Severity: Notice
  Message: Trying to get property of non-object
  Filename: models/payments_model.php
  Line Number: 18
  </code></pre>
  
  <p><strong>Model:</strong></p>
  
  <pre><code>function search_by_room_number($room_number) {
  
      $query = $this-&gt;db-&gt;query("SELECT total FROM new_reservation WHERE room_number = '$room_number' AND payment_status = 'Pending' LIMIT 1"); // Line 16
      $row = $query-&gt;row();
      return $row-&gt;total; // Line 18
  
      $res_id='';
  
      $query = $this-&gt;db-&gt;query("SELECT res_id FROM new_reservation WHERE room_number = '$room_number' AND payment_status = 'Pending' LIMIT 1");
      $row = $query-&gt;row();
      $res_id = $row-&gt;res_id;
  
      $query = $this-&gt;db-&gt;query("SELECT paid_amount FROM new_payment WHERE res_id = '$res_id' LIMIT 1");
      $row = $query-&gt;row();
      return $row-&gt;paid_amount;
  
      $query = $this-&gt;db-&gt;query("SELECT due_amount FROM new_payment WHERE res_id = '$res_id' LIMIT 1");
      $row = $query-&gt;row();
      return $row-&gt;due_amount;
  
  }
  </code></pre>
  
  <p><strong>Controller:</strong></p>
  
  <pre><code>function search_room_number() {
          $popRoomNumbers = $this-&gt;input-&gt;post('room_number');
  
          $totalBill['total'] = $this-&gt;payments_model-&gt;search_by_room_number($popRoomNumbers);
          $previouslyPaid['paid_amount'] = $this-&gt;payments_model-&gt;search_by_room_number($popRoomNumbers);
          $dueAmount['due_amount'] = $this-&gt;payments_model-&gt;search_by_room_number($popRoomNumbers);
  
          $tmpFormData=array_merge($totalBill, $previouslyPaid, $dueAmount);
      $this-&gt;load-&gt;view('/main/new_payment', $tmpFormData);
  }
  </code></pre>
  
  <p><strong>View:</strong></p>
  
  <pre><code>&lt;?php $attr = array(
                  'name'  =&gt;  'totalBill',
                  'id'    =&gt;  'totalBill',
                  'class' =&gt;  'span2',
                  'readonly'=&gt;'true',
                  'value' =&gt;  $total
              );
  echo form_input($attr);
  ?&gt;            
  
  &lt;?php $attr = array(
                  'name'  =&gt;  'previouslyPaid',
                  'id'    =&gt;  'previouslyPaid',
                  'class' =&gt;  'span2',
                  'readonly'=&gt;'true',
                  'value' =&gt;  $paid_amount
              );
  echo form_input($attr);
  ?&gt; 
  
  &lt;?php $attr = array(
                  'name'  =&gt;  'dueAmount',
                  'id'    =&gt;  'dueAmount',
                  'class' =&gt;  'span2',
                  'readonly'=&gt;'true',
                  'value' =&gt;  $due_amount
              );
  echo form_input($attr);
  ?&gt;   
  </code></pre>