Doubt about foreach with mysql data

1

I'm new to php and mysql, I have a database with some fields already created, the idea is to print a ticket like the supermarkets but I want to list the purchased products one by one, so I read that you could use foreach, this it's the code that I have

foreach ($datos as $productos) {
   echo $productos->product_name ." x ". $productos->quantity; 
 }

product_name refers to the field in the bd of the names of the products and quantity the quantity.

Ideally, it would be something like:

soap x 1 potatoes x 2

And so, but I get the following error:

  

Notice: Trying to get property 'product_name' of non-object in

Does anyone have any ideas for this disoriented boy? thanks!

    
asked by Mauricio Lizcano 25.02.2018 в 09:35
source

2 answers

0

First of all I advise you read this and without forgetting to read this question .

At the same time, I summarize the functions that you would use for the recovery and printing of database values in php, since there are 2 programming methodologies (Oriented to Procedures that are eventually becoming obsolete and Oriented to Objects). links, in the examples you will see the uses of both styles, the functions are aliases of the mysqli object methods:

Connect: mysqli :: __ construct (Method _construct of the Class Mysqli )

See: mysqli :: query

Run data and display: mysqli_stmt :: fetch

Close connection: mysqli :: close

    
answered by 26.02.2018 / 02:41
source
1

I already resolved many thanks to all, for those who have a similar problem here I leave as I resolved, thanks again to all:

<?php 
 <br>
 $imprimir =''; <br>
 foreach ($datos as $productos) { <br>
 $imprimir .= '* '.$productos['product_name'].'<br>';
}
    
answered by 27.02.2018 в 06:47