How would i add the sum of a column in mysql?
Here's my code:
$add = mysql_query("SELECT SUM(rsvp) FROM TABLE_NAME WHERE rsvp > 0;")or die(mysql_error());
when i echo it, it gives me a Resource id #
try this instead
$q = mysql_query("SELECT SUM(rsvp) as sum FROM TABLE_NAME WHERE rsvp > 0") or die(mysql_error());
$row = mysql_fetch_assoc($q);
echo $row['sum'];
I recommend looking up more on how to use PHP and MySQL together possibly from one of these sites:
- http://php.net/manual/en/book.mysql.php
- http://www.youtube.com/user/phpacademy - this one is pretty nooby but it does cover everything from pagination to image uploads and beyond. a good place to start I guess.
mysql_query returns a resource, not a value. You need to use another function, such as mysql_fetch_row to access the value it contained:
$result = mysql_query("SELECT SUM(rsvp) FROM TABLE_NAME WHERE rsvp > 0;") or die(mysql_error());
$row = mysql_fetch_row($result); // get an array containing the value of the first row of the above query
$sum = (int) $row[0]; // get an integer containing the value of the first (and, here, only) item in that row
Yes, and you use mysql_fetch_array to fetch a row from that resource.
$resource = mysql_query( ... );
if ($row = mysql_fetch_array($resource))
{
$add = $row[0];
}
I've found the problem,
Note: Undefined index: sum in D:\xampp\htdocs\demo-shop\cart.php on line 9
$sum_query= "SELECT sum(Prod_Tot) as sum FROM cart WHERE Prod_Tot > 0";
$sum_query_res = mysql_query($sum_query);
$row = mysql_fetch_row($sum_query_res);
echo $row['sum'];
If I put echo $row['0']; instead of echo $row['sum']; then it is ok.
ended up figuring it out.
$add = mysql_query("SELECT SUM(rsvp) FROM TABLE_NAME WHERE rsvp >= 1;")or die(mysql_error());
list ( $rsvp_total ) = mysql_fetch_array($add);
echo $rsvp_total;
This is short and simple try it
$sql = mysql_query("SELECT SUM(rsvp) as sum FROM TABLE_NAME WHERE rsvp > 0") or die(mysql_error());
$record = mysql_fetch_assoc($sql);
echo $record['sum'];
Related
I don't understand why this isn't working and i don't know how to fix it:
$sum=0;
$queryone="SELECT SUM(ID) FROM seriestwo";
$result = mysqli_query($link,$queryone);
$row = mysqli_fetch_array($result, MYSQLI_NUM);
while($row = $result->fetch_assoc()){
$sum = $sum + 1;
}
echo $sum
it always prints 0.
What you are doing is wrong because the $result stores only 1 row, that is the sum of ID. You can try two things:
Method 1: Use COUNT() in mysqli. Check MySQL Reference
For eg. check: select count(*) from table of mysql in php
$queryone="SELECT COUNT(ID) as total FROM seriestwo";
$result = mysqli_query($link,$queryone);
$row = mysqli_fetch_assoc($result, MYSQLI_NUM);
echo $row['total'];
Method 2: Use this code:
$sum=0;
$queryone="SELECT `ID` FROM seriestwo";
$result = mysqli_query($link,$queryone);
$row = mysqli_fetch_array($result, MYSQLI_NUM);
while($row = $result->fetch_assoc()){
$sum = $sum + 1;
}
echo $sum;
Which Method to Use
The first method is the one you should be using right now, in this case. You can use second when you want to add extra things, like print every ID
You may be overthinking this.
It's as simple as the following and using an alias while using only 3 lines of code:
$query = $link->query("SELECT SUM(`ID`) as `total` FROM seriestwo");
$row = $query->fetch_assoc();
echo $row['total'];
I need to retrieve the count of a field name called remark corresponding to a particular user.
select count(remark) from attendance where username=_POST['username']
How will I implement this in php? I need to store the count to a variable $totalcount.
This is what i tried out:
$result = mysql_query("select count(1) FROM attendance where stud_id='$_POST['user']'");
$row = mysql_fetch_array($result);
$total = $row[0];
Also quotes are not used properly in the query string. Try this code.
$result = mysql_query("select count(*) FROM attendance where stud_id='".$_POST['user']."'");
$row = mysql_fetch_array($result);
$total = $row[0];
I am trying to show a count of all applications stored in a database with the status of 1.
Here is my UPDATED code:
$result=mysql_query("SELECT * FROM member ")or die('You need to add an administrator ' );
$counter = mysql_query("SELECT COUNT(*) as personID FROM member where state='1' ");
$row = mysql_fetch_array($result);
$personID = $row['personID'];
$num = mysql_fetch_array($counter);
$countadmin = $num["personID"];
However this isn't showing anything when I echo `$countadmin'
Can anyone help
You may try this
$query = mysql_query("select count(*) from member where state='1'");
if ($query) {
$count = mysql_result($query, 0, 0);
echo $count;
}
Check mysql_result and also notice the Warning at top. Also make sure that, the field is state not status, it's confusing, you mentioned status in your question but used state in the query.
Also, following line is not required to get the count of your second query
$result=mysql_query("SELECT * FROM member ")or die('You need to add an administrator ' );
Also, make sure that, you have connected to a database and selected it.
You try to readout "ID", but you select COUNT as "personID"
You have two options here;
$result = mysql_query("SELECT * FROM `member` WHERE `Status`='1'");
$num_rows = mysql_num_rows($result);
or
$result = mysql_query("SELECT COUNT(`Status`) FROM `member` WHERE `Status`='1'");
while($row = mysql_fetch_array($result)){
$Count = $row['count(Status)'];
}
I've got the following query in an existing script - but it's not always returning a value even though it should based off what's in the database. There are plenty of things in the database it SHOULD be grabbing - they are there.
Don't see anything wrong with it - but I barely do this anymore :) See anything?
$query = "SELECT id FROM xtags WHERE tag_id = '$tagid' ORDER BY RAND() Limit 2";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
$query = "SELECT * FROM xtable WHERE id = '$row[id]'";
$result = mysql_query($query) or die(mysql_error());
$row2 = mysql_fetch_assoc($result);
echo $row2[title];
}
$result is being used inside the loop and outside, try making a new variable inside and not reusing the outside one.
You're reusing the $result variable inside the loop which overwrites the value for use in the while condition. Use a different name for $query and $result inside the loop.
I don't know if is ok, but you are using $result twice, one before the "while" and another inside the "while".
I would personally split the string and the variable $row.
Why not use var_dump() to see $row and the other variables ???
I don't understand what you are trying to do actually, but try this:
$query = "SELECT id FROM xtags WHERE tag_id = '".$tagid."' ORDER BY RAND() Limit 2";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
$query2 = "SELECT * FROM xtable WHERE id = '".intval($row[id])."'";
$result2 = mysql_query($query2) or die(mysql_error());
$row2 = mysql_fetch_assoc($result2);
echo $row2[title];
}
Problem solved - did a join statement.
I want to get the number of rows in my MySQL table and store that number in a php variable. This is the code I'm using:
$size = #mysql_query("SELECT COUNT(*) FROM News");
$size ends up being "Resource ID #7." How do I put the number of rows directly into $size?
mysql_query returns a query resource id. In order to get values from it you need to use mysql_fetch_assoc on the resource id to fetch a row into an array.
$result = mysql_query("SELECT COUNT(*) FROM News");
$row = mysql_fetch_assoc($result);
$size = $row['COUNT(*)'];
You need to call mysql_fetch_row or one of its sister functions.
<?php
// untested
$result = #mysql_query("SELECT COUNT(*) FROM News");
// error handling
$row = mysql_fetch_row($result);
$count = $row[0];
?>
try the following:
$size = #mysql_query("SELECT COUNT(*) AS `total` FROM News");
$query = mysql_fetch_array($size);
echo $query['total'];
On a related note you can use the mysql_num_rows() function to obtain the number of rows from a given query. This is handy if you need to grab the data but also know the number of rows.
<?php
$result = #mysql_query("SELECT * FROM news");
$count = #mysql_num_rows($result);
?>