How do I get the 3 latest values from the database.
My codes to show all values is like this :
<?php
$query="select hari from reg";
$hasil=mysql_query($query);
?>
<table style="text-align:center;">
<tr>
<th>Data</th>
</tr>
<?php
if($hasil === FALSE) {
die(mysql_error());
}
while ($data=mysql_fetch_array($hasil)) {
echo ("<tr><td> $data[hari] </td></tr>");
}
?>
</table>
Thanks for the help.
$query="select id , hari from reg ORDERBY id desc LIMIT 3";
This will get you the latest 3 records.
$query="select hari from reg ORDER BY hari DESC LIMIT 3";
$query= SELECT hari FROM reg ORDER BY hari DESC LIMIT 3
Try this
$query = mysql_query("SELECT id,hari FROM reg ORDER BY id DESC LIMIT 0,3") or die(mysql_error());
Consider to use mysqli or PDO_extension --> http://www.php.net/manual/en/intro.mysql.php
Then like all other's recommendation it's possible with ORDER BY and LIMIT 3.
If you would use timestamps per record you could ORDER BY timestamp and LIMIT 3 to them OR if you use an autoincrement int ORDER BY id and LIMIT 3
Mysql also has functions that can return the last entry.. Maybe there is a function to get a specific amount of last entries.
Related
I'm making a PHP code for showing data of my weather station.
I have a DB in MySQL with 5 columns.
This is my actual code:
<?php
$conexion=mysqli_connect('localhost','dbone','root','dbdata');
?>
<?php
$sql="SELECT * from Sensor ORDER BY id DESC LIMIT 1";
$result=mysqli_query($conexion,$sql);
date_default_timezone_set("Europe/Warsaw");
$calpres=57;
$caltemp=0;
$calhumi=0;
while($mostrar=mysqli_fetch_array($result)){
?>
<?php
?>
|esta=c00m000e00|data=<?php echo date("d-m-Y H:i:s"); ?>|temp=<?php echo $mostrar['value1'] - $caltemp ?>|hum=<?php echo $mostrar['value2'] ?>|pres=<?php echo $mostrar['value3'] + $calpres ?>
<?php
}
?>
The actual result is:
|esta=c00m000e00|data=24-01-2021 19:42:10|temp=10.71|hum=58.20|pres=1016.12
Value1 is the column that includes the temperature. I would like to show the maximum and minimum temperature, but I don't know how to do it.
Thank you very much!
You could use SQL MAX() and MIN() functions.
Something like SELECT MAX(value1) FROM Sensor
Either run 2 queries (one for min and one for max) like this:
(if your column name is temp)
SELECT * from Sensor ORDER BY temp DESC LIMIT 1
SELECT * from Sensor ORDER BY temp ASC LIMIT 1
and the take the first and only rows from them.
A little bit cleaner way is to take all the data you need, ordered by temp, (no LIMIT at the end)
SELECT * from Sensor ORDER BY temp DESC
fetch it in an array, and get the first tempArray[0] and the last tempArray[count(tempArray)-1] - values, they will containt min and max rows.
$result = mysql_query("SELECT * from Sensor ORDER BY temp DESC");
$sensorArray = []
while( $row = mysql_fetch_assoc( $result)){
$sensorArray[] = $row;
}
$max = $sensorArray[0];
$min = $sensorArray[count($sensorArray)-1];
I have this code which display a list of players and their points in a contest:
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<?php
$result=mysql_query("SELECT * FROM `pointstable` WHERE `contestfield` = 1 ORDER BY `pointsfield` ASC");
while ($info=mysql_fetch_assoc($result)) {
$playerid=$info['playeridfield'];
$playername=$info['playernamefield'];
$pointsfield=$info['pointsfield'];
?>
<tr><td style="text-align:center; padding-bottom:5px" valign="middle"><?=$playername?> made <?=number_format($pointsfield)?></td></tr>
<?php } ?>
</table>
This will display the complete list, but when a user plays more than one time, it displays all the points he has...
So what I want is to display only the first value (in this case, the lowest points since the objetive of the contest is to get the lowest score) The playeridfield is a unique number for each player, each player has one.
Who can this be done?
Ad this to your SQL query at the end LIMIT 1
Just use the mysql min function and group by id:
SELECT playeridfield, playernamefield, MIN(pointsfield) AS pointsfield FROM `pointstable`
WHERE `contestfield` = 1 GROUP BY `playeridfield` ORDER BY `pointsfield` ASC
Try changing your SQL query to this:
$result=mysql_query("SELECT * FROM `pointstable` WHERE `contestfield` = 1 ORDER BY `pointsfield` ASC LIMIT 1");
The LIMIT 1 should make it so only one row is returned from the query.
I'm trying to get the top 5 users in my database according to their post count
<?php
$data = mysql_query("SELECT COUNT(*) FROM pins ORDER BY user_id ASC LIMIT 5")
or die(mysql_error());
while($info = mysql_fetch_array( $data ))
{
Print "<table><tr><td>";
Print "".$info['user_id']."";
Print "</td></tr></table>";
}
?>
This is a code I have adapted which works for individual users with a WHERE user_id='999' clause. But how do I change it to get the top 5?
Use GROUP BY:
SELECT user_id
FROM pins
GROUP BY user_id
ORDER BY COUNT(*) DESC
LIMIT 5
I have the selecting from the last ten entries working, but am unsure how to get the most popular from these ten entries? Also how would I count the number of the most popular entry & output it to a percentage?
<?php
$sql = "SELECT data FROM table_answers ORDER BY id DESC LIMIT 10";
$result = mysql_query ($sql, $db);
while ($row = mysql_fetch_array ($result))
{
echo "[".$row['data']."]";
}
?>
And I have tried to do the WHERE value as well but it doesn't return any result.
$sql = "SELECT data FROM table_answers WHERE id IN (SELECT id FROM table_answers
ORDER BY id DESC LIMIT 10) ORDER BY popularity DESC LIMIT 1";
$result = mysql_query ($sql, $db);
while ($row = mysql_fetch_array ($result))
{
echo " [".$row['data']."] ";
}
Anyone have any idea what I might be doing wrong here? please
This should solve the problem -
SELECT tableorder.*
FROM (SELECT *
FROM table
ORDER BY id DESC
LIMIT 10) tableorder
ORDER BY tableorder.popularity DESC
LIMIT 1
The inner query will sort on the basis on id and get the top 10. The outer will again sort the 10 rows on the basis of popularity and return the row with highest popularity.
SELECT data
FROM (
SELECT data
FROM table_answers
ORDER BY id DESC
LIMIT 10
) t
ORDER BY popularity
I'm having trouble with my members list. It shows EVERY username but i would like it to show only 15 per row.
The code:
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="23%"><em><strong>Username</strong></em></td>
</tr>
<?
$sql = mysql_query("select * from usr_users");
while($m = mysql_fetch_array($sql))
{ ?>
<tr>
<td width="23%"><div style="float: left;" onMouseover="ddrivetip('<em><? echo("$m[username]");?></em> <br /> <b>Rank:</b> <? echo("$m[rankerslevel]");?><br /> <b>Bits:</b> <? echo("$m[credits]");?><br /> <b>Score:</b> <? echo("$m[points]");?><br /> <b>Mood:</b> <? echo("$m[usrstatus]");?><br /> <b>ID:</b> <? echo("$m[id]");?><br /> <b>Sex:</b> <? echo("$m[sexmalefemale]");?><br /> <b>Country:</b> <? echo("$m[countrywhere]");?><br />','white', 100)";
onMouseout="hideddrivetip()"><img src="/bolt.png" alt="member_icon"/> <font color="<? echo("$m[usercolour]");?>"><? echo("$m[username]");?></font></td></div>
<? } ?>
Thanks in advance!
Use LIMIT clause. Here is usage:
LIMIT [offset,] rows
Examples:
SELECT * from usr_users LIMIT 0, 10
This query will retrieve 1-10 rows (from 0 to 10).
SELECT * from usr_users LIMIT 10, 10
This query will retrieve 11-19 rows.
If you want to get row with specific ids use IN statement:
SELECT * from usr_users WHERE id IN (1,2,3)
Also read this: http://dev.mysql.com/doc/refman/5.0/en/select.html
If you want to learn how to make pagination, look at this topic: http://www.codediesel.com/php/simple-pagination-in-php/
the simpliest soultion would be:
SELECT * FROM usr_users LIMIT 15
use mysql limit
select * from usr_users order by createddate desc LIMIT 0, 15
assuming you have a column called createddate on which you are doing an order by.
This will get you the last 15 users created.
You can get the total number of records and divide it by the "items to be displayed " to get the number of paging elements.
http://www.dharshin.com/2007/09/paging-results-with-php-and-mysql-part.html
Supposing you have an 'id' column - and it is set as Auto_Increment (as-well as an INT)
SELECT * FROM usr_users ORDER BY -id LIMIT 15
This will grab the 15 most latest users, and show them in order.