Cleverbot
Cleverbot

Reputation: 574

Sql, Object vs String/Array

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

Upvotes: 0

Views: 1353

Answers (3)

Cleverbot
Cleverbot

Reputation: 574

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']) . "'
");

Upvotes: 0

Kirzilla
Kirzilla

Reputation: 16596

$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

Upvotes: 1

biziclop
biziclop

Reputation: 14596

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'];
}

Upvotes: 2

Related Questions