i am not sure if i am doing this correctly, and i am kinda stucked here.
Here's what i want to do, each row in the database has 5 column, say A,B,C,D with each having an integer value out of 10 and E which is like a counter. so what i want is to get an average.
which should be something like below
(A + B + C + D)/(E * 4)
and each of these values calculated above, has to be summed up and divide by the total number of rows.
So say i have 3 entry it has to divided by 3
so what i came up with is
$myresult = mysql_query("SELECT * FROM studies WHERE classes = '$classid'");
list($mycount) = mysql_fetch_row($myresult);
$result = mysql_query("SELECT ID, sum((maths + sciences + moral + bm)/(count*4))/'$mycount' FROM studies WHERE classes = '$classid'");
am i doing this right?
if the sql is right, how am i suppose to print out the result so i can use it as a variable?
thank you
The query should be right, but to get the actual value is easy.
You have to fetch_array the query, and the first variable would be the ID and the second would be the sum.
Example
while($row = mysql_fetch_array($query)){
echo $row[0]; // This is the ID
echo $row[1]; // This is the sum
}
Related
I am making an exercise related app. My plans are for day A and day B. I have a database with a list of exercises in it. When I try to search for a given exercise, it is drawn for day A. But when I try to draw it for day B, it happens that the two are the same data. I tried to compare these two data, but I still fail.
$e_legs_B = "SELECT * FROM exercises WHERE body_part = 'Quad' AND difficulty = 'Easy' ORDER BY RAND() LIMIT 1";
$e_legs_B_run = mysqli_query($conn, $e_legs_B);
Then in body my code looks like this.
<?php
$rows = $e_legs_B_run->fetch_assoc();
$name = $rows['name'];
echo "<option value = '$name'>$name</option>";
?>
Select data for day a and b in one query and split the result.
Or store the id's from day a in an array and exclude them in the second query for day b.
I want to get how many (possibly 0) times a particular number occurs in a particular column. I set the number in $contact_client_ID then do the SELECT query below.
$sql = "SELECT * FROM t_contacts WHERE contact_client_ID ='$contact_client_ID'";
$result=(mysqli_query($link, $sql));
$count_result= mysqli_num_rows($result);
echo "contact client ID $contact_client_ID xxxxx $count_result";
Instead of $count_result containing the number I want, it contains a result made up of the number I want and the contact_client_ID joined together and the result doesn't seem to be usable as a number in any following code.
So, if $contact_client_ID = 50 and there are 2 occurrences of it in the table, the output I get is:
contact client ID 50 xxxxx 250
I've looked at the manuals and examples all over the place (including here) and I can't see what I'm doing wrong.
There are multiple ways of doing this, more precisely you have the option, to either do the count in PHP, or make your SQL server do the counting and just return the number:
Do it in PHP:
What it requires is: - fetch all data; -make a counter variable; - loop trough the data, for each loop increase counter +1
For small tables you can use PHP, for bigger ones I advice doing it on the SQL, since for PHP to count it it must fetch all the data.
$counter=0;
$query = "SELECT * FROM t_contacts WHERE contact_client_ID ='$contact_client_ID";
$res = $con->query($query);
while ($row = $res->fetch_assoc()) {
$counter++;
}
Do it in SQL
Now the smarter way would be to do it on the SQL server ,as it would handle the load better;
I would say what /u/Adaleni wrote is pretty close to what I would use:
$sql = "SELECT count(contact_client_ID) as total FROM t_contacts WHERE contact_client_ID ='$contact_client_ID'";
$result=mysqli_query($link, $sql);
$count_result= mysqli_fetch_row(result);
echo "contact client ID $contact_client_ID xxxxx $count_result[0]['total']";
Lets just describe what he does:
we use COUNT() function in SQL, this makes the server count the number of occurances of contact_client_ID and then make (in the result) a new variable called "total"
we execute the query and get the result
We use mysqli_fetch_rowm this function gets the result row as an enumerated array
then we access that array (as we know its only 1 item, we accessed index 0) and we print the variable total which we made in step 1 - $count_result[0]['total']
Try this
/* Select queries return a resultset */
if ($result = $mysqli->query("SELECT * FROM t_contacts WHERE contact_client_ID ='$contact_client_ID")) {
printf("Select returned %d rows.\n", $result->num_rows);
This is very awkward, this is a very simple fetch value, equal to otherwise sum one and then save... but is not working...
I have a predefined value as 1010, so, I select the last entry in the SQL, if the last entry is grater than 1010 then get the value and add 1, then save it which now the last value would be 1011 correct? but is not....
$values_b = 1010;
$getval = 'SELECT number FROM table ORDER BY number DESC LIMIT 1';
$final = $con->query($getval);
$vals = $final ->fetch_array();
$numbers = $vals['number'];
if ($numbers > 1010){
$new_n = $numbers + 1;
$new_numbers = 'INSERT INTO table (number) VALUES ('$new_n')';
$con->query($new_numbers);
} else {
$new_numbers = 'INSERT INTO table (number) VALUES ('$values_b')';
$con->query($new_numbers);
}
Very simple, but the result some how is awkward, it doesn't save 1010 or 1011 or any of the sort... instead is saving 2325 and why is that???? I don't use this values anywhere else, those are unique variables but I really don't understand, this is the first time this is happening, my table is utf-8-general-ci and the column is INT(20) I have try VARCHART(100) but the result is almost the same...
What I'm trying to achieve is "simple", I have a per-defined number 1010, so, if this is the first record save that number 1010, if not, then get the last record say 1020 which mean that there are 10 records, just sum 1 to the last record 1020 + 1 = 1021 and save it... see, very simple, but for some reason is not doing it... so I was wondering if someone can help me solve this...
Thank you for taking the time
SOLVED
The problem here was the query, I was calling to a column with the incorrect name
this table has about 19 columns, the first 5 are
ID | Name | email | numbers ..... the column 16 is | number |
I was calling number when in reality I should be calling for NUMBERS the result that I was getting was from another file, so, in other words, I was getting the result from B and adding 1 to B when I should be using A...
this is entirely my fault, I should paid more attention... sorry guys...
Try this:
$values_b = 1010;
$getval = 'SELECT max(number) as bignumber FROM table';
$final = $con->query($getval);
$vals = $final ->fetch_array();
$numbers = $vals['bignumber'];
if ($numbers > 1010){
$new_n = $numbers + 1;
$new_numbers = 'INSERT INTO table (number) VALUES ('$new_n')';
} else {
$new_numbers = 'INSERT INTO table (number) VALUES ('$values_b')';
}
$con->query($new_numbers);
I am trying to grab the total amount of sales made for a a single month
$aug11 = mysql_query("SELECT SUM(price) FROM table WHERE sales_date LIKE '08/%/2011' ");
when I echo $aug11 I get a resource id# error. I also tried to do this
$test1 = mysql_fetch_array($aug11);
and when I echo $test1 it just says "Array".
Is it absolutely necessary to place a GROUP by sales_date in the original query and then have a while loop that grabs the array and lets me echo the values?
I don't really need any other value except the sum of 'price' for the month of August.
Can someone please explain to me how I can display the value I need without a while loop?
use this to get the sum value
$test1[0];
echo $test1[0]; // returns the sum
$aug11 is not a query string, it is the result of a query. When you fetch an array from the result, it is an array of values, not a scalar. I think you want to do this:
$query = "SELECT SUM(price) FROM table WHERE sales_date LIKE '08/%/2011'";
$aug11 = mysql_query($query);
$row = mysql_fetch_array($aug11);
$test1 = $aug11[0];
With the first query in the following code I am looking for the checkings made in Berlin under the game number two.
With the second query I want to give points for each of the checkings.
As you will see I am using the function SUM. But let's say that I have 2 checkings and the points for each checking are 50. Well, instead of echoing 100, with this code I echo 5050
What is wrong with it?
Thanks a lot
THE CODE IS CORRECTED AND WORKING, JUST IN CASE SOMEBODY NEEDS IT. Thanks to all
$querya = "SELECT * FROM checkins where gamesid=2 and city='Berlin'";
$resulta = mysql_query($querya) or die(mysql_error());
$sumOfPoints = 0;
while($rowa = mysql_fetch_array($resulta)){
$n = $rowa['venuesid'];
$queryb = "SELECT venuesid, SUM(points) as sumpoints from venues where venuesid='".$n."' GROUP BY venuesid ORDER BY venuesid";
$resultb = mysql_query($queryb) or die(mysql_error());
while($rowb = mysql_fetch_array($resultb)){
$sumOfPoints += $rowb['sumpoints'];
}
}
echo "Total points is $sumOfPoints<br/>";
Some suggestions
Use SUM(points) AS sum (only for clarity when use the sum)
Check that points are numerical fields not varchar.
points must be a string, you need to cast it
SUM(cast(points) as int)
and then follow Gaurav's point about labeling the compute
You're getting 5050 because your script is echoing 50 in two iterations of the loop.
From the looks of it, your second query's GROUP BY clause is not grouping on venuesid when it should be. So, first try changing your GROUP BY clause to:
GROUP BY venuesid
If that doesn't give you the result you're looking for, then you should try running your query directly against the database with a static value for venuesid to see what you get back from it, then perhaps update your question and we can take it from there.