php sum of an sql query and an php variable - php

Hey guys i want a sum of an mysql query and an php variable. Is there a function to do this? Here is what i tried:
$result3 = mysql_query($sql3);
while ($resultarray3 = mysql_fetch_array($result3)){
$Ergebnis = $Menge + $resultarray['Bestand'];
echo $Ergebnis;
}
Can anyone help me on this?
Edit: I want a sum of an php variable and an mysql query not of two sql tables!!!!

$menge = '';
$result3 = mysql_query($sql3);
while ($resultarray3 = mysql_fetch_array($result3)){
$menge = $menge + $resultarray3['Bestand'];
}
// result => $menge

If there is only one row you don't need the .=
$result3 = mysql_query($sql3);
while ($resultarray3 = mysql_fetch_array($result3)){
$Ergebnis .= $Menge + $resultarray3['Bestand'];//notice the change on this line
echo $Ergebnis;
}

A query to show a result contains SUM(column), so you could use:
$sql3 = "SELECT bestand, SUM(bestand) as menge FROM database GROUP BY bestand";
And then in PHP
$result3 = mysql_query($sql3);
while ($resultarray3 = mysql_fetch_array($result3)){
echo $resultarray['bestand'] . ' = ' . $resultarray['menge'];
}

You define $resultarray3 but then you use $resultarray without the three.
$Ergebnis = $Menge + $resultarray3['Bestand'];

Related

Display the total of the SUM (price * quantity) from SELECT query [duplicate]

I have a column in a table that I would like to add up and return the sum. I have a loop, but it's not working.
while ($row = mysql_fetch_assoc($result)){
$sum += $row['Value'];
}
echo $sum;
You can completely handle it in the MySQL query:
SELECT SUM(column_name) FROM table_name;
Using PDO (mysql_query is deprecated)
$stmt = $handler->prepare('SELECT SUM(value) AS value_sum FROM codes');
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$sum = $row['value_sum'];
Or using mysqli:
$result = mysqli_query($conn, 'SELECT SUM(value) AS value_sum FROM codes');
$row = mysqli_fetch_assoc($result);
$sum = $row['value_sum'];
$query = "SELECT * FROM tableName";
$query_run = mysql_query($query);
$qty= 0;
while ($num = mysql_fetch_assoc ($query_run)) {
$qty += $num['ColumnName'];
}
echo $qty;
Try this:
$sql = mysql_query("SELECT SUM(Value) as total FROM Codes");
$row = mysql_fetch_array($sql);
$sum = $row['total'];
Let us use the following image as an example for the data in our MySQL Database:
Now, as the question mentions, we need to find the sum of a particular column in a table. For example, let us add all the values of column "duration_sec" for the date '09-10-2018' and only status 'off'
For this condition, the following would be the sql query and code:
$sql_qry = "SELECT SUM(duration_sec) AS count
FROM tbl_npt
WHERE date='09-10-2018' AND status='off'";
$duration = $connection->query($sql_qry);
$record = $duration->fetch_array();
$total = $record['count'];
echo $total;
MySQL 5.6 (LAMP) . column_value is the column you want to add up. table_name is the table.
Method #1
$qry = "SELECT column_value AS count
FROM table_name ";
$res = $db->query($qry);
$total = 0;
while ($rec = $db->fetchAssoc($res)) {
$total += $rec['count'];
}
echo "Total: " . $total . "\n";
Method #2
$qry = "SELECT SUM(column_value) AS count
FROM table_name ";
$res = $db->query($qry);
$total = 0;
$rec = $db->fetchAssoc($res);
$total = $rec['count'];
echo "Total: " . $total . "\n";
Method #3 -SQLi
$qry = "SELECT SUM(column_value) AS count
FROM table_name ";
$res = $conn->query($sql);
$total = 0;
$rec = row = $res->fetch_assoc();
$total = $rec['count'];
echo "Total: " . $total . "\n";
Method #4: Depreciated (don't use)
$res = mysql_query('SELECT SUM(column_value) AS count FROM table_name');
$row = mysql_fetch_assoc($res);
$sum = $row['count'];
$row['Value'] is probably a string. Try using intval($row['Value']).
Also, make sure you set $sum = 0 before the loop.
Or, better yet, add SUM(Value) AS Val_Sum to your SQL query.
$result=mysql_query("SELECT SUM(column) AS total_value FROM table name WHERE column='value'");
$result=mysql_result($result,0,0);
Get Sum Of particular row value using PHP MYSQL
"SELECT SUM(filed_name) from table_name"
$sql = "SELECT SUM(Value) FROM Codes";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){
sum = $row['SUM(price)'];
}
echo sum;

I'm not sure where im wrong. Just 5 lines of code

All I need to do, is count all fields of the table, where postid=38.
But my code always prints "1" on screen, no matter what number I write in postid=38.
<?
$consulta2 = mysql_query("select count(*) from $tabla_db4 where postid='38';");
$result2 = mysql_num_rows($consulta2);
echo (string) $result2;
?>
You can do either this:
<?
$consulta2 = mysql_query("select count(*) as count from $tabla_db4 where postid='38';");
$result2 = mysql_fetch_array($consulta2);
echo $result2['count'];
?>
OR
$consulta2 = mysql_query("select* from $tabla_db4 where postid='38';");
$result2 = #mysql_num_rows($consulta2);
echo $result2;
Solved! Please close.
$sql = mysql_query(
"SELECT * FROM `comentarios` WHERE `postid` =
$registro[id] ORDER BY `postid` DESC",$conexion_db);
$cuenta = mysql_num_rows($sql);
echo $cuenta;
You can try this if you want to know how many rows there are:
<?php
$query = "select count(*) as amount from $tabla_db4 where postid='38';";
$result = mysql_query($query);
if(!$result) {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}
while($row = mysql_fetch_assoc($result)) {
echo $row['amount'];
}
mysql_free_result($result);
?>
Also, are you sure postid is a string, since you're putting it in between quotes. It looks like it might be an integer. Mind the datatypes.

SELECT Count php/sql

I am trying to store a mysql value into a php variable. I have the following query which I know works. However, I the value for $count is always 0. Can someone explain what I need to do to get the count value? The count should be the count of x's w here name_x=.$id.
$query = "SELECT COUNT(name_x) FROM Status where name_x=.$id.";
$result = mysql_query($query);
$count = $result;
Is first letter in table name is really capital. Please check it first.
or Try :
$query = "SELECT COUNT(*) as totalno FROM Status where name_x=".$id;
$result = mysql_query($query);
while($data=mysql_fetch_array($result)){
$count = $data['totalno'];
}
echo $count;
$query = "SELECT COUNT(*) FROM `Status` where `name_x`= $id";
$result = mysql_query($query);
$row = mysql_fetch_row($result);
$count = $row[0];
please try it
$query = "SELECT COUNT(*) FROM Status where name_x=$id";
$result = mysql_query($query);
$count = mysql_result($result, 0);
You are missing single quotes around $id. Should be
name_x = '" . $id . "'";

multiple query use resultset of previous query in one php file

I am trying to use a value that I receive from a MySQL query and then do an insert but it's not working. I'm getting an syntax error but the Insert Query is correct.
The select query returns an amount which I'm checking and then the program should do the insert query.
<?php
require 'header.php';
$resID = mysql_real_escape_string($_POST['resID']);
$materialen_id = mysql_real_escape_string($_POST['materialen_id']);
$aantal = mysql_real_escape_string($_POST['aantal']);
$effectief_gebruikt = mysql_real_escape_string($_POST['effectief_gebruikt']);
$opmerking = mysql_real_escape_string($_POST['opmerking']);
//$datum_van = date('d-m-Y', $_POST['datum_van']);
//$datum_tot = date('d-m-Y', $_POST['datum_tot']);
$datum_van = $_POST['datum_van'];
$datum_tot = $_POST['datum_tot'];
$sql = "SELECT `aantal_beschikbaar`
FROM `materialen`
WHERE `id` = $materialen_id";
$result = mysql_query($sql) or die(mysql_error());
while ($row = mysql_fetch_array($result))
{
$tot = $row['aantal_beschikbaar'];
echo 'totaal: ' . $tot;
}
$sql2 = "SELECT `aantal` FROM `materialen_per_reservatie`
WHERE `materialen_id` = $materialen_id";
$result2 = mysql_query($sql2) or die(mysql_error());
while ($row = mysql_fetch_array($result2))
{
//$aant = $row['aantal'];
//echo $aant
echo $row['aantal'];
}
$besch = ($tot - $aant);
echo 'beschikbaar: ' . $besch;
/*$sql3 = "SELECT * FROM `materialen_per_reservatie`
WHERE `reservaties_id` = $resID
AND `materialen_id` = $materialen_id";
$result3 = mysql_query($sql3) or die(mysql_error());*/
if($besch > $aantal){
$string2 = "INSERT INTO `materialen_per_reservatie`(`reservaties_id`, `materialen_id`, `aantal`, `effectief_gebruikt`, `opmerking`, `datum_van`, `datum_tot`) VALUES ($resID, $materialen_id, $aantal, $effectief_gebruikt, '$opmerking', '$datum_van', '$datum_tot')";
mysql_query($string2) or die(mysql_error());
}
require 'footer.php';
?>
Provided that the error is on only the insert query...
Your insert query is missing a needed space:
INSERT INTO `materialen_per_reservatie` (`reservaties_id`, `materialen_id`, `aantal`, `effectief_gebruikt`, `opmerking`, `datum_van`, `datum_tot`) VALUES ($resID, $materialen_id, $aantal, $effectief_gebruikt, '$opmerking', '$datum_van', '$datum_tot')
Add a space after materialen_per_reservatie. And I'm not sure you need all of the quotes.
INSERT INTO materialen_per_reservatie (reservaties_id, materialen_id, aantal, effectief_gebruikt, opmerking, datum_van, datum_tot) VALUES ($resID, $materialen_id, $aantal, $effectief_gebruikt, '$opmerking', '$datum_van', '$datum_tot')

Get sum of MySQL column in PHP

I have a column in a table that I would like to add up and return the sum. I have a loop, but it's not working.
while ($row = mysql_fetch_assoc($result)){
$sum += $row['Value'];
}
echo $sum;
You can completely handle it in the MySQL query:
SELECT SUM(column_name) FROM table_name;
Using PDO (mysql_query is deprecated)
$stmt = $handler->prepare('SELECT SUM(value) AS value_sum FROM codes');
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$sum = $row['value_sum'];
Or using mysqli:
$result = mysqli_query($conn, 'SELECT SUM(value) AS value_sum FROM codes');
$row = mysqli_fetch_assoc($result);
$sum = $row['value_sum'];
$query = "SELECT * FROM tableName";
$query_run = mysql_query($query);
$qty= 0;
while ($num = mysql_fetch_assoc ($query_run)) {
$qty += $num['ColumnName'];
}
echo $qty;
Try this:
$sql = mysql_query("SELECT SUM(Value) as total FROM Codes");
$row = mysql_fetch_array($sql);
$sum = $row['total'];
Let us use the following image as an example for the data in our MySQL Database:
Now, as the question mentions, we need to find the sum of a particular column in a table. For example, let us add all the values of column "duration_sec" for the date '09-10-2018' and only status 'off'
For this condition, the following would be the sql query and code:
$sql_qry = "SELECT SUM(duration_sec) AS count
FROM tbl_npt
WHERE date='09-10-2018' AND status='off'";
$duration = $connection->query($sql_qry);
$record = $duration->fetch_array();
$total = $record['count'];
echo $total;
MySQL 5.6 (LAMP) . column_value is the column you want to add up. table_name is the table.
Method #1
$qry = "SELECT column_value AS count
FROM table_name ";
$res = $db->query($qry);
$total = 0;
while ($rec = $db->fetchAssoc($res)) {
$total += $rec['count'];
}
echo "Total: " . $total . "\n";
Method #2
$qry = "SELECT SUM(column_value) AS count
FROM table_name ";
$res = $db->query($qry);
$total = 0;
$rec = $db->fetchAssoc($res);
$total = $rec['count'];
echo "Total: " . $total . "\n";
Method #3 -SQLi
$qry = "SELECT SUM(column_value) AS count
FROM table_name ";
$res = $conn->query($sql);
$total = 0;
$rec = row = $res->fetch_assoc();
$total = $rec['count'];
echo "Total: " . $total . "\n";
Method #4: Depreciated (don't use)
$res = mysql_query('SELECT SUM(column_value) AS count FROM table_name');
$row = mysql_fetch_assoc($res);
$sum = $row['count'];
$row['Value'] is probably a string. Try using intval($row['Value']).
Also, make sure you set $sum = 0 before the loop.
Or, better yet, add SUM(Value) AS Val_Sum to your SQL query.
$result=mysql_query("SELECT SUM(column) AS total_value FROM table name WHERE column='value'");
$result=mysql_result($result,0,0);
Get Sum Of particular row value using PHP MYSQL
"SELECT SUM(filed_name) from table_name"
$sql = "SELECT SUM(Value) FROM Codes";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){
sum = $row['SUM(price)'];
}
echo sum;

Categories