Advertisement
tok124

php select array

May 13th, 2022
1,241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.68 KB | None | 0 0
  1. <?php
  2.   $conn = mysqli_connect("127.0.0.1", "root", "ascent", "world");
  3.  
  4.   $stmt = $conn->prepare("SELECT * FROM tablename");
  5.   $stmt->execute();
  6.   $result = $stmt->get_result();
  7.   if($result->num_rows > 0) {
  8.     while($rows = $result->fetch_all(MYSQLI_ASSOC)) {
  9.       $price = array_column($rows, 'price'); // <== price is column name of the price column. So you may replace this if the name of column is not price
  10.       $sumprice = array_sum($price);
  11.       foreach($rows as $row) {
  12.         echo nl2br("item name: ".$row['name']." Price: ".$sumprice."\n"); // You may also replace name in $row['name'] with another column name... Or you can just simply remove it
  13.       }
  14.     }
  15.   }
  16. ?>
  17.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement