How to sort MySQL results and then get the ID of the latest 10 datetimes in PHP -
how can take id , timestamp results mysql $row[id]
, $row[timestamp]
, sort results , array of id's latest matching timestamps? if mysql database has data in it,
3,2015-08-24 10:38:31
, 34,2015-08-24 10:38:16
, 8,2015-08-24 10:38:51
i end array, of id's in order newest oldest timestamps.
8,3,34
here code information omitted.
$servername = "ip:port"; $username = "username"; $password = "password"; $dbname = "db"; $conn = new mysqli($servername, $username, $password, $dbname); if ($conn->connect_error) { die("connection database failed. please try again. error: " . $conn >connect_error); } $sql = "select id, timestamp posts order timestamp desc limit 1,10;"; $result = $conn->query($sql); $arrays = array(); if ($result->num_rows > 0) { while($row = $result->fetch_assoc()) { $arrays[] = $row[id]; } } foreach ($arrays $key => $id) { $servername = "ip:port"; $username = "username"; $password = "password"; $dbname = "db"; $conn = new mysqli($servername, $username, $password, $dbname); if ($conn->connect_error) { die("connection database failed. please try again. error: " . $conn->connect_error); } $sql = "select name,timestamp,text posts id=\"" . $id . "\";"; $result = $conn->query($sql); $arrays = array(); if ($result->num_rows > 0) { while($row = $result->fetch_assoc()) { $date = date_create_from_format('y-m-d h:i:s',$row[timestamp]); $timezone = new datetimezone('america/los_angeles'); $timedate = date_timezone_set($date, $timezone); $finaltimedate = $timedate->format('y-m-d h:i:s a'); echo "<hr><h4>" . $row[name] . "<span style=\"float:right\">time posted: " . $finaltimedate . "</span></h4><br>"; echo $row[text] . "<br>"; } } }
however, when reload page, shows 2 of same, id=0, blog post rather post 0 , 4 (only 2 posts).
if understand question, query should work you.
select id, timestamp table_name order timestamp desc limit 1,10;
Comments
Post a Comment