How to sum of MySQL table column in php? - php

I want to get the sum total of the table columns in my database.
I've tried using the following code but have not been successful.
$link=mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_NAME);
$result = mysqli_query($link,'SELECT SUM(value) AS value_sum FROM User_Table');
$row = mysqli_fetch_assoc($result);
$sum = $row['value_sum'];
echo $sum;
Thank you very much!!

I hope you try to find total number of record of a table of User_Table
$link=mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_NAME);
$result = mysqli_query($link,'SELECT SUM(your_column_name) AS value_sum FROM User_Table');
//or like the query for return last row that indicate total number of record
// id auto increment
$result = mysqli_query($link,'SELECT * FROM User_Table ORDER BY id DESC LIMIT 1;');
$row = mysqli_fetch_assoc($result);
$sum = $row['id'];
echo $sum;
// or using count
$result = mysqli_query($link,'SELECT COUNT(*) total_row FROM User_Table;');
$row = mysqli_fetch_assoc($result);
$sum = $row['total_row '];
echo $sum;

As of my understanding you need count the number of columns your database have. If I am not wrong, you may please use the query below
select * from information_schema.columns
where table_schema = '<YOUR DATABASE NAME>'
order by table_name,ordinal_position
Hope this helps. Thanks

Related

i want to select a randomly data using php

I need help in selecting a random data from the table
Let's suppose There are 10 columns in my table but i want that if I hit the query sometime it will bring all data and sometime it will bring data only from 1 column and some time from the no 1 and no 2 column
How can I achieve this?
Thanks in advance
function selectAllInventoriesINGroup($conn,$groupname,$import_id)
{
$cgstSgst = explode('-', $groupname);
$sql = "SELECT * FROM stockitems WHERE generalid = '$import_id' AND cgst = '$cgstSgst[0]' AND sgst = '$cgstSgst[1]' ORDER BY RAND()";
$result = $conn->query($sql);
$jsonxx = mysqli_fetch_all ($result, MYSQLI_ASSOC);
return $jsonxx ;
}
I have tested this but it fetch the all the data from the database
<?php
$sql = "SELECT * FROM stockitems WHERE generalid = '$import_id' AND cgst = '$cgstSgst[0]' AND sgst = '$cgstSgst[1]' ORDER BY RAND()";
?>

Displaying Row Count for Data in PHP

Been at this awhile...this is actually another simplified question to a problem I posted earlier. I'm trying to display the country and count for the following query:
$query = mysqli_query($con, "SELECT COUNT(id), country FROM users GROUP BY country ORDER BY COUNT(id) DESC");
while ($row = mysqli_fetch_array($query)) {
$countries = $row['country'];
echo $countries;}
What I'm getting in output is the countries listed from highest to lowest, but it does not have the count displayed next to it. I know these have been summed in the query, how do I target the number & assign it to a php variable for display?
Any help would be appreciated.
You can use an alias for your COUNT column and then reference that in the array returned by mysqli_fetch_array for each row.
$query = mysqli_query($con, "SELECT COUNT(id) AS countryCount, country FROM users GROUP BY countryCount ORDER BY countryCount DESC");
while ($row = mysqli_fetch_array($query)) {
$count = $row['countryCount'];
$countries = $row['country'];
echo $count;
echo $countries;}
You need to add alias in query for COUNT(id) like below:-
$query = mysqli_query($con, "SELECT COUNT(id) as count, country FROM users GROUP BY country ORDER BY count DESC");
Now change while() loop code a bit:-
while ($row = mysqli_fetch_assoc($query)) { // _assoc for lighter array iteration
$countries = $row['country'];
$countries_count = $row['count'];// get count of each country
echo $countries.'--'.$countries_count ;// show country and it's count both
}
Is this what you are trying to do:-
$query = mysqli_query($con, "SELECT COUNT(id) AS total_country, country FROM users GROUP BY country ORDER BY id DESC");
while ( $row = mysqli_fetch_array( $query ) )
{
$countries = $row['country'];
$total_countries = $row['total_country'];
echo "<p>{$countries}</p>;
echo "<p>Total count: {$total_countries}</p>";
}
You're not assigning the variable.
Do this: $count = $row['COUNT']; and echo it. It should work

How to select the maximum value of a column in MySQL

I want to select the maximum value of a column of my table. I'm using PHP and MySQL. This is what I have so far:
$max = "SELECT MAX(Id) FROM trialtabel2";
$max1 = mysqli_query($dblink, $max);
echo= $max1;
My debugger is just saying that it is a query returning a 0 boolean value (false). I cannot find a specific answer anywhere on the internet.
You need to fetch the data from the mysqli_result object that was returned to you when you executed your query using mysqli_query.
$max = "SELECT MAX(Id) as id FROM trialtabel2";
$max1 = mysqli_query($dblink, $max);
$row = mysqli_fetch_assoc($max1); // this was missing
$id=$row['id'];
echo $id;
Note: I removed the loop because with MAX query without any grouping you will get only 1 row returned. If you had multiple rows in result set you would need to loop through all of them
two ways
first is as people described
$max = "SELECT MAX(Id) as max_id FROM trialtabel2";
$max1 = mysqli_query($dblink, $max);
$row = mysqli_fetch_assoc($max1);
$max_id=$row['max_id'];
echo $max_id;
second is ordering and limiting
$max_id = 0;
$max = "SELECT id FROM trialtabel2 order by id desc limit 0,1";
$max1 = mysqli_query($dblink, $max);
while($row = mysqli_fetch_assoc($max1)){
$max_id=$row['id'];
}
echo $max_id;
In Your code you missing the fetch statement. you need to fetch from the resultset. see above what you are missing.
Try using this..,
$max = "SELECT MAX(Id) as maxId FROM trialtabel2";
$max1 = mysqli_query($dblink, $max);
$row = mysqli_fetch_assoc($max1);
echo $row['maxId'];
Hope this helps..
$max = "SELECT Id FROM trialtabel2 order by id desc limit 1";
$max1 = mysqli_query($dblink, $max);
$result = mysqli_fetch_assoc($max1);
pre($result);

Sum up all the qty from a column

I have column qty in my table, and I want to sum up all the qty from this column in a variable:
$get_sold = "select qty from pending_orders where product_id='$p_id' AND order_status=='done'";
$result = mysqli_query($con, $get_sold);
try this.
$get_sold = "select sum(`qty`) as sum from pending_orders where product_id='$p_id' AND order_status='done'";
$result = mysqli_query($con,$get_sold);
$row=mysqli_fetch_assoc($result);
echo $row['sum'];
First you need sum to your column in your query by using sum(qty)as totals and also remove == from order_status it is =
you need to use mysqli_fetch_row to get your sum data into your variable
<?php
$get_sold = "select sum(`qty`)as totals from `pending_orders` where `product_id`='".$p_id."' AND `order_status`='done'";
$result = mysqli_query($con,$get_sold);
$row=mysqli_fetch_row($result);
$row[0];// get your total sum
try using, MySql sum function
$get_sold = "select sum(qty) total_qty from pending_orders where product_id='$p_id' AND order_status='done'";
$result = mysqli_query($con,$get_sold);
you should use
select sum(qty) as sumqty from pending_orders where product_id='$p_id' AND order_status='done'
Also mention, that in mysql you need to write on equal sign instead of two
Most people have responded here with a quick copy/paste, but I feel like it would be better suited to format your query nicely for readability sake to help you identify the issue.
$get_sold = "
SELECT SUM(`qty`) as `total_quantity`
FROM `pending_orders`
WHERE `product_id` = '$p_id' AND `order_status` = 'done'
";
$result = $con->query($get_sold);
if ( !$result ) {
die("MySQL error: " . $con->error);
}
$row = $result->fetch_assoc();
$quantity = $row['total_quantity'];
CHANE QUERY , SUM(column_name) gives sum of all numbers in column_name
$get_sold = "select SUM(qty) as total from pending_orders where product_id='$p_id' AND order_status='done'";

need help returning sql sum of $variable table name

I'm trying to return the sum of balances from a table and can get the following code to work when using a specific table name
$qry = mysql_query("SELECT SUM(Balance) AS total FROM table1 ");
$row = mysql_fetch_assoc($qry);
echo $row['total'];
The problem I'm having is that my table name changes and this needs to be a variable but when I use the following code I get no result
$table="table1";
$qry = mysql_query(" SELECT SUM(Balance) AS total FROM $table ");
$row = mysql_fetch_assoc($qry);
echo $row['total'];
Can anyone offer some help please?
How about:
$qry = mysql_query(" SELECT SUM(Balance) AS total FROM " . $table );

Categories