2017-02-26 49 views
0

我有一個表格,它具有我想要以json格式檢索的值並放在android中。我的問題是我如何限制1的值只有1?我如何限制取自mysql的值?

這是我的代碼

$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"}]} 
+0

SELECT * FROM新聞ORDER BY n_date DESC LIMIT 1 –

+0

它限制一切都變成1,我只需要1的值被限制爲1和別人不侷限於1只 – orange

+1

你的問題讓沒有意義。你想限制一個「日期」,但這個例子沒有日期。您應該提供清晰的樣本數據和期望的結果。 –

回答

0

如果你想限制數據,你可以通過添加一個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

+0

它將所有內容限制爲1個,我只需要將n_date限制爲1而不是所有內容 – orange