I have the following query and it gives me the percentage. It works great and i echo it in a table. In this table i also want to echo the AVG % as a total.i.e i have 12 months of year and want the avg. % for the year at the bottom of the table. I have worked out how to do this for valuations and instructions that are stored in my database. But.. how do i do this for a 'virtual' column like %..as this has been created from a query and not a physical column in the database.
iam starting to think i need to combine the total valuations by total instructions to get my %. Any ideas how i can do this?
$query= "SELECT *,
concat(round(( instructions/valuations * 100 ),0),'%') AS percentage
FROM office_figures2016";
$result = mysqli_query($conn, $query);
while($office_figures2016=mysqli_fetch_assoc($result)){
it echos :
echo"".$office_figures2016['percentage']."";
and here is the php to get the totals for each column...the third part of code is wrong but this is what i have so far:
<?php
$sql = "SELECT ROUND(AVG(valuations),0) AS value_sum FROM office_figures2016";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<td> " . $row["value_sum"]."</td>";
$sql = "SELECT ROUND(AVG(instructions),0) AS value_sum FROM office_figures2016";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<td> " . $row["value_sum"]."</td>";
// need to divide total valuations by total instructions x 100 = %
// somehow need to combine the value sums together.. if?
$sql = "SELECT ROUND(AVG(''),0) AS value_sum FROM office_figures2016";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<td> " . $row["value_sum"]."</td>";
I redid the formula to divide all instructions and valuations in the table x100 = %.
$sql = "SELECT ROUND(AVG(instructions / valuations *100 ),0) AS value_sum FROM office_figures2016";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<td> " . $row["value_sum"]."</td>";
Related
I'm trying to code a php script, which can find if the last 10 rows from mySQL database are the same. And if the values are same, then the script could sent an email.
I can fetch the last 10 rows.
$sql = ("SELECT ID, DayTime,Temperature FROM AllinOne ORDER BY ID DESC LIMIT 10");
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "id: " . $row["ID"]. " - DayTime: " . $row["DayTime"]. " - Temperature " . $row["Temperature"]. "<br>";
}
} else {
echo "0 results";
}
Last 10 results:
Now, I'm trying to check if last 10 rows in "Temperature" column are the same (as we can see).
I use:
$sql2 = "SELECT Temperature, COUNT(*) as duplicates FROM AllinOne GROUP BY Temperature ORDER BY ID DESC LIMIT 10";
$result2 = $conn->query($sql2);
if ($result2->num_rows > 0)
{
// output data of each row
while($row2 = $result2->fetch_assoc())
{
echo $row2["duplicates"]. "<br>";
}
}
but I can't take back the same results (i can't understand the final result).
$row2 results
Could you help me???
Execute the following query and grab the result:
SELECT
COUNT(*) AS count_rows,
MIN(Temperature) AS min_temperature,
MAX(Temperature) AS max_temperature
FROM (
SELECT *
FROM t
ORDER BY DayTime DESC
LIMIT 10
) AS x
Then send an email if count_rows = 10 and min_temperature = max_temperature.
You just need to fix your second query like:
$sql2 = "SELECT Temperature, COUNT(*) as duplicates
FROM (
SELECT ID, DayTime, Temperature FROM AllinOne ORDER BY ID DESC LIMIT 10
) LastData
GROUP BY Temperature";
Here you can test PHP code with queries
So, let’s say I have 10 rows in my database. However, after the mysql query, only 9 rows are being displayed in the html table.
I need every row from the database to be displayed in the html table.
I hope someone can point me in the right direction. I’ll be here if I need to provide anymore details.
This is my code:
<?php
$sqlQuery = "SELECT * FROM periods";
$result = mysqli_query($conn, $sqlQuery);
$resultCheck = mysqli_num_rows($result);
if ($resultCheck > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$id = $row['id'];
$out = $row['fell_out'];
$in = $row['fell_in'];
$sum = $row['sum'];
$nextEstimate = $row['next_estimate'];
$nextEstimateDays = $row['next_estimate_days'];
$notes = $row['notes'];
$sqlQueryLastDate = "SELECT * FROM (select * from periods WHERE id < $id ORDER BY id DESC LIMIT 1) AS x ORDER BY id LIMIT 1";
$resultLastDate = mysqli_query($conn, $sqlQueryLastDate);
$resultCheckLastDate = mysqli_num_rows($resultLastDate);
if ($resultCheckLastDate > 0) {
while ($rowLastDate = mysqli_fetch_assoc($resultLastDate)) {
$lastInDate = $rowLastDate['fell_in'];
$sqlQueryCurrentDate = "SELECT * FROM (select * from periods WHERE id = $id ORDER BY id DESC LIMIT 1) AS x ORDER BY id LIMIT 1";
$resultCurrentDate = mysqli_query($conn, $sqlQueryCurrentDate);
$resultCheckCurrentDate = mysqli_num_rows($resultCurrentDate);
if ($resultCheckCurrentDate > 0) {
while ($rowCurrentDate = mysqli_fetch_assoc($resultCurrentDate)) {
$currentOutDate = $rowCurrentDate['fell_out'];
$lastIn = new DateTime($lastInDate);
$currentOut = new DateTime($currentOutDate);
$intervalLastCurrent = $lastIn->diff($currentOut);
$elapsedLastCurrent = $intervalLastCurrent->format('%a days %h hours');
/*Why? Php is erasing everything after adding the above variable to the table...Entire first row gets erased!*/
echo "
<tr>
<td>".$id."</td>
<td class='test'>".$elapsedLastCurrent."</td>
<td class='dateOutResult'>".$out."</td>
<td class='dateInResult'>".$in."</td>
<td class='sumHours'>".$sum."</td>
<td class='nextEstimate'>".$nextEstimate." (".$nextEstimateDays.")</td>
<td class='notes'>".$notes."</td>
</tr>";
} /*$sqlQueryCurrentDate*/
}
} /*$sqlQueryLastDate*/
}
} /*$sqlQuery*/
}
?>
Here's the live result: https://unidrones.co.za/periods
I am really new to mysql/php so I do not understand what I'm doing wrong. The nested queries is probably not the best approach to use, but I'm still learning, and any input will be deeply appreciated.
This is because your query is
//select * from periods WHERE id < $id ORDER BY id DESC LIMIT 1;
your first $id will be always equal id.
I would like to take data from DB via simple script
<a href='category.php?CAT=Shoes'>Shoes</a>
then simple show all rows with the specific data in "CAT" column like this:
$CAT = $_GET['CAT'];
$sql = "SELECT * FROM Shop WHERE CAT = $CAT" ;
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo"
... results
"}}
The problem is that this script does work with INT (for example SELECT CAT = 5 like category.php?CAT=5) but not with VARCHAR (SELECT CAT = Shoes like category.php?CAT=Shoes). Now I'm not sure why is this happening.
With Error: Trying to get property of non-object
$sql = "SELECT * FROM Shop WHERE CAT = '$CAT'"
You need pass $cat as string
$cat = $_GET['CAT'];
$sql = "SELECT * FROM Shop WHERE CAT = '{$cat}'" ;
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo '<pre>' . print_r($row, true) . '</pre><br/>';
}
}
GOAL:
Select from the database to target a team name from one of my five specific columns. the table has a number of columns 5 of which are headed respondent_team_1 to respondent_team_5. In each of those columns there maybe a string value that is repeated so the idea is to look at that columns and know this and just take the 1 distinct value and not all so I can match up the data like so:
CODE:
<?php
$sql = "SELECT distinct respondent_team_1 FROM respondent_data WHERE dashboard_id = $user_dash_id";
$result = $conn->query($sql);
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
echo $row["respondent_team_1"];
}
}
?>
<br>
<?php
$sql = "SELECT distinct respondent_team_2 FROM respondent_data WHERE dashboard_id = $user_dash_id";
$result = $conn->query($sql);
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
echo $row["respondent_team_2"];
}
}
?>
<br>
<?php
$sql = "SELECT distinct respondent_team_3 FROM respondent_data WHERE dashboard_id = $user_dash_id";
$result = $conn->query($sql);
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
echo $row["respondent_team_3"];
}
}
?>
<br>
<?php
$sql = "SELECT distinct respondent_team_4 FROM respondent_data WHERE dashboard_id = $user_dash_id";
$result = $conn->query($sql);
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
echo $row["respondent_team_4"];
}
}
?>
<br>
<?php
$sql = "SELECT distinct respondent_team_5 FROM respondent_data WHERE dashboard_id = $user_dash_id";
$result = $conn->query($sql);
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
echo $row["respondent_team_5"];
}
}
?>
SUMMARY:
What I am trying to do is refine all those code blocks into one code block as A it is messy and B there must be a better way but I am unable to find one?
I have tried various methods, the obvious being take the column names I need and make a select against them, I tried to use the GROUP BY keyword with no luck either. Im not looking for somebody to do this for me but just some wisdom or advice would be excellent.
I have a table with a column called 'status'. The defaut value of 'status' is 0. I want to update the value to '1' after using it.
I basically want to check if the status is 0, if it is, do an operation and then change the value to 1.
Here is the code. All works perfectly except that the value of 0 is not changed to 1.
I am a novice so maybe is a very basic mistake :(
<?php
$sql = "SELECT number, status FROM summonerid";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$SummonerID = $row["number"];
$status = $row["status"];
if($status=='0'){
$recentgames=$lol->getRecentGames($SummonerID);
$MatchID1=$recentgames->games[0]->gameId;
$sql = "INSERT INTO matchid (number) SELECT * FROM (SELECT '$MatchID1') AS tmp WHERE NOT EXISTS (SELECT number FROM matchid WHERE number = '$MatchID1') LIMIT 1;";
$sql = "UPDATE summonerid SET status='1' WHERE status='0';"; // THIS IS THE PART THAT DOES NOT WORK WELL
}
}
}
?>
Any help would be highly appreciated
Try this.. you are not executing the sql statements
$sql = "SELECT number, status FROM summonerid";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$SummonerID = $row["number"];
$status = $row["status"];
if($status=='0'){
$recentgames=$lol->getRecentGames($SummonerID);
$MatchID1=$recentgames->games[0]->gameId;
$sql = "INSERT INTO matchid (number) SELECT * FROM (SELECT '$MatchID1') AS tmp WHERE NOT EXISTS (SELECT number FROM matchid WHERE number = '$MatchID1') LIMIT 1;";
$result1 = $conn->query($sql);
$sql = "UPDATE summonerid SET status='1' WHERE status='0';";
$result1 = $conn->query($sql);
}
}
}
?>