Welcome Guest, Not a member yet? Register   Sign In
consulting variable
#1

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 ^^
Reply
#2

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
Reply
#3

(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
Reply
#4

I thought I did give an example? The above should work assuming the initial code you supplied is correct.
Reply
#5

(This post was last modified: 04-06-2015, 03:10 PM by pompeu.)

(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 ^^
Reply
#6

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();

Reply
#7

(This post was last modified: 04-06-2015, 03:35 PM by pompeu.)

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 ^^
Reply




Theme © iAndrew 2016 - Forum software by © MyBB