1

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,

8
  • 3
    Have you tried printing out the composed query string before passing it to the query() function to see if it matches what you expect? Have you tried running that printed query on the database manually? Commented Aug 9, 2013 at 21:47
  • The error message provided doesn't make sense for the code you provided unless $this is not an object. Commented Aug 9, 2013 at 21:51
  • Are you sure that $product is a string ? Isn't it an object, triggering some __toString() method when used in a concatenation context ? Commented Aug 9, 2013 at 21:54
  • yea, print_r($product); first Commented Aug 9, 2013 at 21:56
  • 1
    What Tomas meant is : put you sql statement into a variable, display/log it and then use it for your query. eg. $sql = 'select max() etc'; echo $sql; then $query = $this->db->query($sql); Commented Aug 9, 2013 at 22:06

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

4 Comments

How does that relate to the trying to get property of non-object error?
some advice about SQL injection? is $product sanitized?
Hi, still error: Trying to get property of non-object $product def not being passed correctly. Thanks.
@Smudger See my updated answer regarding the Active record usage

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.