CodeIgniter Forums

Full Version: how to output MySQL query results?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Tony W[/eluser]
In my controller, I have the following code:

Code:
$query = $this->db->query("SELECT example FROM slang WHERE `slang` =' $slang_input' ");
    

      
      foreach ($query->result() as $row)
      {
        echo $row->example;
      }

I wonder how to output the $row->example better? There might be lots of results for this query, maybe I should make it an array and pass it to the view file?

thanks

El Forum

[eluser]mironcho[/eluser]
In the DB class already exists result_array() method, so you can do:
Code:
$ret = array();

$query = $this->db->query("SELECT example FROM slang WHERE `slang` =' $slang_input' ");
if ($query->num_rows() > 0)
{
    // use result() for array of objects
    // $ret = $query->result();

    // use result_array() for array of arrays
    $ret = $query->result_array();
}

return $ret;

and then loop through it in your view.