Jump to content

how to get only the value of an array PHP prestashop 1.7


xbKappa

Recommended Posts

I have this code for counting product attributes 

$getProductAttributes = Db::getInstance()->executeS('SELECT COUNT(id_product_attribute) FROM '._DB_PREFIX_.'product_attribute WHERE id_product = '.$id_product.'');

print_r($getProductAttributes);

 

and is printing this  Array ( [0] => Array ( [COUNT(id_product_attribute)] => 90 ) )

how can i get only the value 90?

 

Link to comment
Share on other sites

It should be like this,
 

$getProductAttributes = Db::getInstance()->executeS('SELECT COUNT(id_product_attribute) FROM `'._DB_PREFIX_.'product_attribute` WHERE id_product='.(int)$id_product);
print_r($getProductAttributes, true);

then get the value from array.

Edited by Rhobur
clarity (see edit history)
  • Like 1
  • Thanks 1
Link to comment
Share on other sites

Don't use executeS but getValue

 

$getProductAttributes = Db::getInstance()->getValue ('SELECT COUNT(id_product_attribute) FROM '._DB_PREFIX_.'product_attribute WHERE id_product = '.(int)$id_product);

and then $getProductAttributes  = 90

for security, don't forget (int) before $id_product

  • Like 1
  • Thanks 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...