Home > OS >  How to echo Sum of mysqli table
How to echo Sum of mysqli table

Time:07-17

I'm trying to echo the sum from "user_price" so that I can see my revenue. This is my code so far

<?php

                  $query = "SELECT SUM(user_price) from users ";
                  ?>
                  
                  <p class='card-title'><?php echo $row['SUM(user_price)']?><p>"

enter image description here

CodePudding user response:

Figured it out :D

Here it is if anyone ever needs it

<?php
                  $query = "SELECT sum(user_price) as revenue from users";
                  $res = mysqli_query($connection, $query);
                  $data = mysqli_fetch_array($res)
                  

                  ?>
                  
                  <p class='card-title'><?php echo $data['revenue'];?></p>

CodePudding user response:

Looks like you are missing some code to perform the query, or you have not shown it. The best way to get a field from an expression is to give it a column name to refer to, see my change below:

$query = "SELECT SUM(user_price) AS revenue from users ";

Then you can refer to the row like so:

 <p class='card-title'><?php echo $row['revenue']?><p>
  • Related