简体   繁体   English

我如何限制取自mysql的值?

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

I have a table which has values i want to retrieved in json format and put in android. 我有一个表,该表具有要以json格式检索的值并放入android。 My question is how do i limit 1 value to only 1? 我的问题是如何将1的值限制为仅1?

Here is my code 这是我的代码

$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);

?>

Example is for n_date i only want to display the date 1 time. 例子是n_date我只想显示日期1时间。 But it keeps on looping. 但是它不断循环。 How do i limit the date to only 1 where everything else loops? 我如何将日期限制为只有其他所有循环的日期?

Example is like this 例子是这样的

      {"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"}]}

If you want to limit the data, you can change your SQL query by adding a LIMIT: 如果要限制数据,可以通过添加LIMIT来更改SQL查询:

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

If you need more information, you can look into the MySQL Reference Manual: 如果您需要更多信息,可以查阅《 MySQL参考手册》:

The LIMIT clause can be used to constrain the number of rows returned by the SELECT statement. LIMIT子句可用于约束SELECT语句返回的行数。

Source: https://dev.mysql.com/doc/refman/5.5/en/select.html 来源: 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