EjM
EjM

Reputation: 103

error in sql where condition on codeigniter

By executing below query i getting this error message

You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'status=1' at line 1 SELECT username,useremail FROM tbl_cart where user_id= 8AND status=1

query

 $query = $this->db->query('SELECT username,useremail FROM tbl_cart where 
 user_id= '.$this->session->userdata('userId').'AND status=1' ); 


     $resultdata['results'] = $query->result_array();

Upvotes: 0

Views: 243

Answers (2)

Parker Dell
Parker Dell

Reputation: 468

The better way to do it with CI is

$query = $this->db->select('username, useremail')
->where('user_id', $this->session->userdata('userID'))
->where('status',1)
//instead of from CI does get
->get('tbl_cart')->result_array();

result_array() returns the results as an array of arrays while result returns query as an array of objects

if you are using CI it is better to fully take advantage of the query builder classes

Upvotes: 0

EstevaoLuis
EstevaoLuis

Reputation: 2552

You need to insert an space before AND

$query = $this->db->query('SELECT username,useremail FROM tbl_cart where 
user_id= '.$this->session->userdata('userId').' AND status=1' );
                                               ^ here

Upvotes: 2

Related Questions