Reputation: 10771
I am trying to pass the name of a column as a variable to a PHP database query.
the hard coded syntax works perfectly and is:
select max([257612cr]) as price from Price_TekwaniPrice where customeraccount='DAY001'
When I pass the variable I get error trying to get property ofnon object
. my syntax is:
$query = $this->db->query("
select max(['$product']) as price from Price where customeraccount='$customer'
");
I also tried:
$query = $this->db->query("
select max(".$product.") as price from Price where customeraccount='$customer'
");
I have confirmed that the variables are being passed correctly. the syntax for '$customer'
works perfectly so just passing the $product
variable as a column name is proving cumbersome.
I am using php with codeigniter. any advice welcome.
Thanks as always,
Upvotes: 1
Views: 1152
Reputation: 64466
No need to concatenate a php variable when already opened double quotes try this
$query = $this->db->query("
select max([$product]) as price from Price where customeraccount='$customer'
");
or
$query = $this->db->query("
select max($product) as price from Price where customeraccount='$customer'
");
While about the error you are getting is due i think your database driver is not loaded first try to load database
$this->load->database('default', TRUE);
The best way to use CI's Active record you can do so
$this->db->select_max($product);
$this->db->where('customeraccount', $customer);
$query = $this->db->get('Price');
See Active Record
Upvotes: 1