I'm trying to get the sum of a column.
My Schema is as below...
I'd like to get the SUM of 'bill'.
I have the following...
<?php
$uid = $_SESSION['oauth_id'];
$query = mysql_query("SELECT * FROM `users`, `income`, `outgoings` WHERE users.oauth_uid = '$uid' and income.user_id = '$uid' and outgoings.user_id = '$uid'") or die(mysql_error());
$result = mysql_fetch_array($query);
?>
Outgoings = <?php echo $result["SUM(total)"];
I'm not receiving any output, however. Can anybody see where I'm going wrong? There's definitely data in my table.
The SUM function must be used when deciding what to return from the select. Like so.
SELECT SUM(`bill`) FROM `users`, `income`, `outgoings` WHERE users.oauth_uid = '$uid' and income.user_id = '$uid' and outgoings.user_id = '$uid'
Try this if you don't want to do a SUM() query as already proposed by others:
<?php
$sum = 0;
while ($row = mysql_fetch_array($query)) {
$sum += $row['bill'];
}
?>
Outgoings = <?php echo $sum; ?>
But remember that you will need this if you want to reuse the same $query resultset:
<?php
mysql_data_seek($query , 0);
?>
Why not just query for the SUM directly? Like:
<?php
$uid = $_SESSION['oauth_id'];
$sum = mysql_query("SELECT SUM(`bill`) FROM `users` WHERE users.oauth_uid = '$uid'") or die(mysql_error());
?>
This query will get you the sum:
SELECT sum(bill) FROM `the_table`
Related
i'm trying to sum a column name "total". and i want to display the total sorting by id. if user A login he can see total booking in his account.
I keep get the error:
"Notice: Array to string conversion in Array."
can someone help me? I want to echo the total in input form.
this is my php code:
<?php
include ('connect.php');
$sql = "SELECT * FROM penjaga WHERE p_username = '".$_SESSION['username']."'";
$result = mysqli_query($conn,$sql);
$row = mysqli_fetch_assoc($result);
$id = $row['p_id'];
$sql2 = "SELECT SUM(total) as total FROM sitter_kucing WHERE sitter_fk = '$id'";
$row2 = mysql_fetch_array($sql);
$sum = $row['total'];
?>
Try this,
$sql2 = "SELECT SUM(total) as total FROM sitter_kucing WHERE sitter_fk = '$id'";
$result2 = mysql_query($sql2) or die(mysql_error());
$row2 = mysql_fetch_array($result2) or die(mysql_error());
$sum = $row['total'];
i got it! thanks this is my code
this is the code:
<?php
include ('connect.php');
$sql8 = "SELECT * FROM penjaga WHERE p_username = '".$_SESSION['username']."'";
$result8 = mysqli_query($conn,$sql8);
$row8 = mysqli_fetch_assoc($result8);
$id = $row8['p_id'];
$sql9 = "SELECT SUM(total) as total FROM sitter_kucing WHERE sitter_fk = '$id'";
$result9 = mysqli_query($conn,$sql9);
$row9 = mysqli_fetch_array($result9);
$sum = $row9['total'];
?>
I need to do a SELECT * FROM table_X , the problem is table_X is the result of another query, I don't know how to do it, perhaps with two loop, something like this :
<?php
$query1 = mysql_query("SELECT * FROM table_ref");
while ($row = mysql_fetch_array($query1))
{
$name = $row['table_name'];
$query2 = mysql_query(" SELECT * FROM '$name' ");
while ($row = mysql_fetch_array($query2))
{
$time = $data['itime'];
echo $time;
}
}
?>
The tables are all similar & I can't do joint there's no keys. So what I want is to show only the results of the second query from each results of the first query !
So, what's your structure? I don't understand. You have column table_name where are listed a lot of tables? If so, just use backquotes on your $name:
$query2 = mysql_query(" SELECT * FROM `$name` ");
Apart from the obvious that has been pointed out in the comments, you're overwriting $row in the second loop.
Also, you're trying to read an array ($data) that is not defined.
The following will work much better (but still isn't ideal):
$query1 = mysql_query("SELECT `table_name` FROM `table_ref`");
while ($row = mysql_fetch_array($query1))
{
$name = $row['table_name'];
$query2 = mysql_query("SELECT `itime` FROM `$name`");
while ($data = mysql_fetch_array($query2))
{
$time = $data['itime'];
echo $time;
}
}
just change your quotes to have the query ready to be started
change
$query2 = mysql_query(" SELECT * FROM '$name' ");
to
$query2 = mysql_query(" SELECT * FROM `".$name."` ");
i would also rather sugest to check this part
while ($row = mysql_fetch_array($query2))
{
$time = $data['itime'];
echo $time;
}
you already used variable $row to fetching previus query so better to change to something else, look like $data is matching your needs because you already used but you did not declare it
while ($data = mysql_fetch_array($query2))
{
$time = $data['itime'];
echo $time;
}
Try this query:
select x.* from ( SELECT table_name FROM table_ref) as x
I have php code like that:
$q = $db->query("SELECT * FROM tabe1_data WHERE status='1' ORDER BY id DESC LIMIT 10");
$cr_onr = array();
while($row = $db->fetchAll($q))
{
$cr_onr[] = $row;
}
?>
<?php foreach($cr_onr as $new_cr_onr):?>
<?php $crinfo = $db->fetchOne("SELECT * FROM tabe2_data WHERE id ='".$new_cr_onr['op_id']."'");?>
<p><?=$new_cr_onr['username'];?> has been complete <?=$crinfo['op_amount'];?></p>
<?php endforeach;?>
I need to take $crinfo = $db-> out of "foreach" to be before "foreach"
How i can do that please ?
Thank you.
You could join the tables instead of having many queries:
$q = $db->query("
SELECT
*
FROM
tabe1_data
INNER JOIN
tabe2_data ON tabe1_data.op_id = tabe2_data.id
WHERE
tabe1_data.status='1'
ORDER BY
tabe1_data.id DESC
LIMIT 10
");
$cr_onr = array();
while($row = $db->fetchAll($q))
{
$cr_onr[] = $row;
}
?>
<?php foreach($cr_onr as $new_cr_onr):?>
<p><?=$new_cr_onr['username'];?> has been complete <?=$new_cr_onr['op_amount'];?></p>
<?php endforeach;?>
Single joined query?
SELECT tabe2_data.*
FROM tabe2_data
LEFT JOIN tabe1_data ON tabe1_data.op_id = tabe2_data.id
WHERE tabe1_data.status='1'
Am trying to calculate the number of rows in a table depending on a certain condition.
So, I did manage to write a piece of code that would function as required.
But, it's bit too long. So am wondering if there is any easier way to achieve it.
Code:
// Comments
$sql_total_comments = mysql_query("SELECT * FROM comments");
$sql_pending_comments = mysql_query("SELECT * FROM comments WHERE comment_status = '0'");
$sql_approved_comments = mysql_query("SELECT * FROM comments WHERE comment_status = '1'");
$sql_declined_comments = mysql_query("SELECT * FROM comments WHERE comment_status = '2'");
$total_comments_num = mysql_num_rows($sql_total_comments);
$pending_comments_num = mysql_num_rows($sql_pending_comments);
$approved_comments_num = mysql_num_rows($sql_approved_comments);
$declined_comments_num = mysql_num_rows($sql_declined_comments);
SELECT comment_status, COUNT(comment_status)
FROM comments GROUP BY comment_status;
In your code, either total the counts up for the total number or run a separate query on COUNT(*) as in the other answers.
So your code would look something like this, assuming you're using PDO:
$sql = 'SELECT comment_status, COUNT(comment_status) AS "count" '.
'FROM comments GROUP BY comment_status;';
$query = $db->query($sql);
$count_total = 0;
$count = array( );
while (($row = $query->fetch(PDO::FETCH_ASSOC)) !== FALSE) {
$count_total += $row['count'];
$count[$row['comment_status']] += $row['count'];
}
$query = null; // Because I'm neurotic about cleaning up resources.
Use COUNT() DB FUNCTION to do such job.
$ret = mysql_query("SELECT COUNT(*) FROM comments");
$row = mysql_fetch_row($ret);
$total_comments_num = $row[0];
Use select count(*) from comments where .....
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 . "'";