Welcome Guest, Not a member yet? Register   Sign In
Get one result from DB
#1

[eluser]MaxEisley[/eluser]
Hello,

when i am selecting all cars, its ok, but when i need to get just one car, there is a problem.

This is my datamodel:
Code:
function getcar ($id) {
      $q = $this->db->query('SELECT *
      FROM car
      WHERE car_ID = '.$id);
      
      if ($q->num_rows() > 0) {
        foreach ($q->result() as $row) {
          $data[] = $row;
        }
        return $data;
      }
    }
This is constructor:
Code:
$data['car'] = $this->data_model->getcar($this->uri->segment(3));
When i echo $this->uri->segment(3) i get 1 or any other number.

And this view:
Code:
<?php echo $car->car_ID; ?>

And it writes this error:
Message: Trying to get property of non-object

Maybe i am badly getting one record.

Thanks for help.
#2

[eluser]InsiteFX[/eluser]
Code:
// get car - get a single car by id
function get_car($id)
{
    $this->db->limit(1);
    $this-db->where('car_ID', $id);
    $query = $this->db->get('car');      

    if ($query->num_rows() > 0)
    {
        return $query->row();
    }
}

// get cars - get all cars
function get_cars()
{
   $data = array();

    $query = $this->db->get('car');

    if ($query->num_rows() > 0)
    {
        foreach ($query->result_array() as $row)
        {
            $data[] = $row;
        }
    }

    $query->free_result();
    return $data;
}

InsiteFX
#3

[eluser]oppenheimer[/eluser]
In your Model, try this code:

Code:
function getcar ($id) {
      $q = $this->db->query('SELECT *
      FROM car
      WHERE car_ID = '.$id);
      
      return ($q->row());
}

I think it should work with the rest of your code.
#4

[eluser]MaxEisley[/eluser]
Yeah it works thanks.




Theme © iAndrew 2016 - Forum software by © MyBB