Q: URI's to fetch database data (CodeIgniter) So I'm trying to utilise CodeIgniter's URI segment functionality. I have the URI segments being used as ids to retrieve information from the database.
It's working fine, but I have noticed that if I put in an id that doesn't exist in the database, it spits out a database error.
How do I prevent this from happening?
Model Code:
$query = $this->db->select('*')
    ->from('questions')
    ->where('questions.id', $question_id)
    ->join('users', 'users.id = questions.user_id')
    ->get();
return $query->row();

Call to model in controller:
$question_id = $this->uri->segment(3);
$data['question'] = $this->forum_model->get_question($question_id);

PHP error:

A PHP Error was encountered
  Severity: Notice
  Message: Trying to get property of non-object


A: You need to check if the query returned any results before calling $query->row
The way to do it is:
$query = $this->db->select('*')
    ->from('questions')
    ->where('questions.id', $question_id)
    ->join('users', 'users.id = questions.user_id')
    ->get();
    if($query->num_rows() > 0)
          return $query->row();
    else
          return false;

$query->num_rows() will give you the number of rows returned.

A: to debug just do this... 
    //if this gives u no result, then thats the problem because it couldnt find the 
    //$this->uri->segment(3);
$question_id = $this->uri->segment(3);
echo $question_id; 

//if this prints out the expected result.
//then you should know that the problem is still somewhere down the code.
$data['question'] = $this->forum_model->get_question($question_id);
var_dump($data['question']);


//this is for your model.
echo $question_id
$query = $this->db->select('*')
    ->from('questions')
    ->where('questions.id', $question_id)
    ->join('users', 'users.id = questions.user_id')
    ->get();
return $query->row();

NOTE: if all this echos outputs valid values, then check somewhere else for the problem
