• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Getting confused with where in database

#1
Hi there,

I am trying to use a table that has 'img_loc1' and 'img_loc2' and so on and within that table it has the name of the images to use.  I also have an images table that will also have the image names and alt and other info.  I am trying to find a way that I can join the 2 databases to get all the other information.

So for now I am trying to call in the controller the following:

PHP Code:
$data['image_locator'] = $this->Test_model->get_image_location_test('img_loc1'); 

for the time being I am just passing in the img_loc1 to get things to work.  In the model I have done the following:

PHP Code:
public function get_image_location_test($loc){
 
      
       $this
->db->select('*');
 
      $this->db->from('image_locator');
 
      $this->db->join('images''images.image_name = image_locator.img_loc1');  #need to figure this out.
 
      $this->db->where('images.image_name'$loc);
 
      $query $this->db->get();
 
      return $query->row();
 
      
   


but all I am getting is an error of Message: Trying to get a property of non-object.  I have been trying for ages to figure it out and I cannot.  Please bear in mind that I am quite new to php and i am still learning.

Thanks,

doomie
Reply

#2
Hi

Please load model in your controller.

Like
        $this->load->model('[YourModelName]');
Reply

#3
You error, "Trying to get property of non-object", is probably coming from your call to $query->row(), because no result was returned. What you always need to do is test that a row was returned, like so:


PHP Code:
if( $query->num_rows() == )
{
  return $query->row();
}

return 
NULL

As for the join, if images.image_name is the same as  image_locator.img_loc1, then you're doing it right. It doesn't seem like you are though, as usually you will have an integer for a relationship between two tables, one being the index, and the other being the foreign key. This is just speculation though, as we have no idea about your table schemas.

Bhavesh could be right too though. PHP will always give you an error that has good debugging info, so use that to your advantage. If the line number in the error matches your call to the model, then Bhavesh is right. If the line number in the error message matches the line with $query->row(), then just do what I mentioned above.
Reply

#4
(02-07-2016, 11:25 PM)Bhavesh Wrote: Hi

Please load model in your controller.

Like
        $this->load->model('[YourModelName]');

Thank for your reply, I have already got it loaded in the autoloader.php file.

Thanks,
Reply

#5
try $this->test_model (i think that CI translate model name to lowercase)
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.