I need to get the property of the object from the results of the database query. Here is the query:

class Sales { public static function get_one_sales ($number_n) { $db = new DB; return $db->query("SELECT * FROM `sale` WHERE `number`='$number_n'", "Sales"); } } 

Here is the appeal

 $tt = Sales::get_one_sales($number_n); foreach($tt as $rows_sale): $rows_sale->contragent_id; endforeach; 

Through the cycle, everything works fine, but I think that using foreach to output only one value is at least silly and not Feng Shui. Tell me, please, if it is not difficult, direct on the right path.

  • Why do you think that using a loop for an array is stupid? Where it is worse to substitute a variable into a query, use the PDO and prepare the query for this purpose. And the loop for the place where the array can come is not stupid. Well, or add to the query LIMIT 1 - korytoff

1 answer 1

If the result of Sales::get_one_sales($number_n) is an array with strings (and most likely one line), then you can try this:

 $tt = Sales::get_one_sales($number_n); $tt[0]->contragent_id; 
  • Thank you very much RaZik. Saved))) - Igor Chichkov