0

Why does this produce an object?

$sql = $this->db->query("
  SELECT 'product_id'
  FROM " . DB_PREFIX . "product_to_category
  WHERE category_id = '" . $this->db->escape($category['category_id']) . "'
");

And how to I get a string/array instead?

From Opencart Related: Object of class stdClass could not be converted to string

3
  • 2
    According to usage and table name it is class from OpenCart. Commented Jun 25, 2012 at 17:45
  • Yeah it's Opencart, I think they used CodeIgniter to build it. What other info will be helpful? Commented Jun 25, 2012 at 17:50
  • 1
    @Cleverbot, see /admin/models/country.php - there are a lot of good examples about working with db layer. Also, there a lot of examples in other model files. PS: Don't forget to escape string data. Commented Jun 25, 2012 at 17:54

3 Answers 3

2

Assuming CodeIgniter:

http://codeigniter.com/user_guide/database/queries.html

$this->db->query();

The query() function returns a database result object when "read" type queries are run, which you can use to show your results.

You can process the result set rows as array, using result_array:

http://codeigniter.com/user_guide/database/results.html

$query = $this->db->query("YOUR QUERY");

foreach ($query->result_array() as $row){
   echo $row['title'];
   echo $row['name'];
   echo $row['body'];
}
Sign up to request clarification or add additional context in comments.

1 Comment

I get "Call to undefined method stdClass::result_array()". It's totally bizarre I'm not getting a string/array like it normally does.
1
$sql = $this->db->query("
  SELECT 'product_id'
  FROM " . DB_PREFIX . "product_to_category
  WHERE category_id = '" . $this->db->escape($category['category_id']) . "'
");
$rows = $sql->rows; //array of all returned values
$row = $sql->row; //first row

I highly recommend you to use var_dump($sql) to investigate what public object fields could be useful for you.

*Fixed syntax

4 Comments

That totally turned it into an array thanks!! One other thing, what if my return is a single string? should I cut out "$rows = #sql->$rows;"?
$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "country"); return $query->row['total']; Note $query->row, not $query->rows.
That works but I was looking to extract a single string value from the database, or a single string value from the array from the database. When I try your initial method it just gives me the name of the value as opposed to the value itself.
This is my dump: 'Array ( [0] => Array ( [product_id] => product_id ) [1] => Array ( [product_id] => product_id ) [2] => Array ( [product_id] => product_id ) )'
0

You have an error in your query syntax, you put SELECT 'product_id' and you should not have apostrophies there.

$sql = $this->db->query("
  SELECT 'product_id'
  FROM " . DB_PREFIX . "product_to_category
  WHERE category_id = '" . $this->db->escape($category['category_id']) . "'
");

You need to make this change.

$sql = $this->db->query("
  SELECT product_id
  FROM " . DB_PREFIX . "product_to_category
  WHERE category_id = '" . $this->db->escape($category['category_id']) . "'
");

Comments

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.