Welcome Guest, Not a member yet? Register   Sign In
Really basic Database Class question
#1

[eluser]opel[/eluser]
I may have miss read the manual on the Database Class but when I try to run a query like the example below I am unable to write out a statement like $values['thisvalue'] without first running a foreach loop

Code:
$query = $this->db->query("SELECT * FROM table");
$values = $query->result();


I have looked at manual and all examples have a foreach loop but wondered if there is a method that allows you to get straight to an array statement ?
#2

[eluser]Colin Williams[/eluser]
Quote:but wondered if there is a method that allows you to get straight to an array statement ?

Your question is fundamentally flawed. The result() method does return an array of objects. If you want an array of arrays, use result_array(). If you want a single object, use row(). And finally, if you want a single array, use row_array().
#3

[eluser]opel[/eluser]
Sorry I wrote this very late but thanks for your answer.
#4

[eluser]opel[/eluser]
I think I should better explain my question and what I am trying to do. I am trying to make a selection from the database

Code:
$sql = "SELECT * FROM company WHERE company_user_id = ".$this->session->userdata('userid')."";
        
        $query = $this->db->query($sql);
        
        $result = $query->result_array();

if($result['company_id'] == 1)
{
   echo "this";
}
else
{
   echo "here";
}


However the problem I am having is that when you return items they are in a DB class object and to do the following statement I have to write the code as

Code:
if($result[0]['company_id'] == 1)

I just wondered if there was a way to do it like the 1st example ?
#5

[eluser]xwero[/eluser]
If you only need one row you use the row_array method and then you will be able to do what you want.




Theme © iAndrew 2016 - Forum software by © MyBB