There is a request:

$res = mysqli_query($link, "SELECT sum(price) FROM items"); $sum=mysqli_fetch_assoc($res); echo ($sum); 

But he displays ARRAY. How do I get the sum of the numbers in the cell?

  • one
    suddenly, but echo ($sum['sum(price)']) - Alexander Belinsky
  • oh damn, ahahaha, thank you - Senbonzakuraa
  • one
    you can also mysqli_fetch_assoc .... in the request to do alias: SELECT sum(price) as price and get as $ sum ['price'] - Alexey Shimansky

2 answers 2

Or so

 $res = mysqli_query($link, "SELECT sum(price) FROM items"); $row = mysqli_fetch_array($res); echo $row[0]; 

Directory

  • @Mcile, and why is this answer wrong? - Visman
  • @Mcile, if you don’t know how mysqli_fetch_array() works, then you don’t have to go with your allegations! Follow the link and read Выбирает одну строку из результирующего набора и помещает ее в ассоциативный массив, обычный массив или в оба - Visman

Well, as a crutch option, I think it will do:

 $res = mysqli_fetch_assoc(mysqli_query($link, "SELECT sum(price) sum_price FROM items")); echo $res['sum_price']; 
  • Thank you, reminded me of the stupid questions I asked. - Senbonzakuraa