簡體   English   中英

我如何限制取自mysql的值?

[英]How do i limit a value taken from mysql?

我有一個表,該表具有要以json格式檢索的值並放入android。 我的問題是如何將1的值限制為僅1?

這是我的代碼

$sql = "SELECT * from news  Order by n_date Desc";
 $con = mysqli_connect($server_name,$mysql_user,$mysql_pass,$db_name); 


 $result = mysqli_query($con,$sql);


 $response = array();
while($row=mysqli_fetch_array($result))
{
array_push($response, array("NID"=>$row['NID'], "title"=>$row[2], "content"=>$row[3], "n_date"=>(new DateTime($row[4]))->format('M d, Y l g:i:s A')));
 }



echo json_encode (array("news_response"=>$response));




mysqli_close($con);

?>

例子是n_date我只想顯示日期1時間。 但是它不斷循環。 我如何將日期限制為只有其他所有循環的日期?

例子是這樣的

      {"news_response":[{"NID":"2","title":"2016","
    content":"Hello World ",
    "n_date":"Feb 17, 2017 Friday 11:01:57 PM"},{"NID":"1","title":"Hello there",
"content":"Hello there hehe"}]}

如果要限制數據,可以通過添加LIMIT來更改SQL查詢:

$sql = "SELECT * FROM news ORDER BY n_date DESC LIMIT 1";

如果您需要更多信息,可以查閱《 MySQL參考手冊》:

LIMIT子句可用於約束SELECT語句返回的行數。

來源: https : //dev.mysql.com/doc/refman/5.5/en/select.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM