CodeIgniter Forums

Full Version: consulting variable
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

how to save a sql query in a variable ? , for example:

I have the following query:
Code:
$country = $this-> db- > query ("select country from users where email = '$email' and password = '$password' " ) ;

I want to store that user's country in a variable : Example $var_country = Brazil
Code:
$var_country = $country;

But when I use the variable gives the following error:

Code:
A PHP Error was encountered

Severity : 4096

Message : Object of class CI_DB_mysql_result could not be converted to string

Thanks ^^
You didn't grab the result from the executed query and access the 'country' column from the result
PHP Code:
$country $this-> db- > query ("select country from users where email = '$email' and password = '$password' " ) ;

//execute the query
$executed $country->result(); //normally I'd add ->result() to the above query...

//grab the country field from the result
$var_country $executed->country
(04-06-2015, 02:06 PM)CroNiX Wrote: [ -> ]You didn't grab the result from the executed query and access the 'country' column from the result

PHP Code:
$country $this-> db- > query ("select country from users where email = '$email' and password = '$password' " ) ;

//execute the query
$executed $country->result(); //normally I'd add ->result() to the above query...

//grab the country field from the result
$var_country $executed->country

sorry, did not get it right , could explain me better , could give a real example of how would this query ?

thanks mate
I thought I did give an example? The above should work assuming the initial code you supplied is correct.
(04-06-2015, 02:57 PM)CroNiX Wrote: [ -> ]I thought I did give an example? The above should work assuming the initial code you supplied is correct.

the above code returns an error:

Code:
A PHP Error was encountered

Severity: Notice

Message: Trying to get property of non-object

Filename: controllers/login.php

Line Number: 21

follows as did

Code:
        //Conex√£o e consulta com DB
        $query = $this->db->query("select email, country, pass from users where email = '$email' and pass = '$pass'");
        $executed = $query->result();
        $country = $executed->country;
        echo $country;

Thanks ^^
Your original query ($query) most likely failed. Did you supply a valid $email and $pass?

Try this (for testing):
PHP Code:
$query $this->db->query("select email, country, pass from users where email = '$email' and pass = '$pass'")->result();
if (
$query//test if query was successful
{
  echo 
'Country = ' $query->country;
}
else
{
  echo 
'ERROR in Query:<br>' $this->db->last_query();

Cronix,

I had not got it right the concept of using the queries in CI but is now resolved. I like that and it worked :

Code:
$query = $this-> db-> query ("select email, country , password from users where email = '$email' and password = '$password' " ) ;
foreach ( $query->result() as $row )
{
$country = $row->country;
}

Thanks very much

Problem resolved ^^