php获取最后一组数据 php 取数组第一个

php读取mysql,符合条件的数据有多条时,想只取最后一条数据,代码怎么写?

$query = "select * from userlist where username = '$lune' ORDER BY id DESC LIMIT 1"; 

创新互联作为成都网站建设公司,专注重庆网站建设、网站设计,有关成都企业网站定制方案、改版、费用等问题,行业涉及成都餐厅设计等多个领域,已为上千家企业服务,得到了客户的尊重与认可。

$result = mysqli_query($link, $query); 

$row = mysqli_fetch_array($result); 

echo $row['gangwei'];

最简单的方式,既然有ID,直接通过ID排序,取一条即可.

PHP如何读取数据库记录中的最后一条数据?

您使用了limit 0,1就只有一条了,就是你要的最后一条。也许你问的问题没有说清楚

在PHP中使用SQL语句 怎么取出查询出来的最后一个数据?

select a.name name1,a.*,b.value,b.* from piwik_site a,piwik_archive_numeric_2013_10 b where b.period in(1,2,3) and b.name='nb_visits' and a.idsite=b.idsite order by b.value desc

如果页面显示的是这个语句,那么最后一个应该是select a.name name1,a.*,b.value,b.* from piwik_site

a,piwik_archive_numeric_2013_10 b where b.period in(1,2,3) and

b.name='nb_visits' and a.idsite=b.idsite order by b.value asc limit 1

或者先执行一次select count(*) 拿到数量num,再select a.name name1,a.*,b.value,b.* from piwik_site

a,piwik_archive_numeric_2013_10 b where b.period in(1,2,3) and

b.name='nb_visits' and a.idsite=b.idsite order by b.value desc limit num-1,1

php怎么获取数据库最后几条数据

$con = mysql_connect("localhost","root","");//连接数据库

mysql_select_db("btxiazai",$con);//选择数据库

mysql_query("set names utf8");

$sql = "select * from persons order by id desc limit 2";//获取persons中的数据,并按id倒叙排列,取其中两条

$get = mysql_query($sql);//执行sql

while($result = mysql_fetch_assoc($get)){//取回数据

}


分享文章:php获取最后一组数据 php 取数组第一个
本文路径:http://scyanting.com/article/hgscjg.html