189 8069 5689

php数据库取前面 php获取数据库的某条信息

php查询MYSQL数据库 前5条内容 并显示出来

?php

西城网站建设公司创新互联公司,西城网站设计制作,有大型网站制作公司丰富经验。已为西城数千家提供企业网站建设服务。企业网站搭建\外贸网站制作要多少钱,请找那个售后服务好的西城做网站的公司定做!

header("content-type:text/html;charset=utf-8");

$conn = mysql_connect('localhost','root','123456');

mysql_select_db('dbname',$conn);

mysql_query("set names utf8");

$sql = "select infoname from tablename limit 5 order by id desc";

$res = mysql_query($sql,$conn);

echo "html";

echo "head";

echo "/head";

echo "body";

echo "ul";

while($row = mysql_fetch_assoc($res)){

echo "li".$row['infoname']."/li";

}

echo "/ul";

echo "/body";

echo "/html";

mysql_free_result($res);

mysql_close($conn);

?

php查询数据库的前5条数据,用数组存起来

为了便于随时echo,存为字符串最好,一般的代码如下:

$sql='select * from xxx order by xxid desc limit 5';//limit 5表示只取5个,order by xxxid desc表示按xxxid降序排列,可以显示最新的5个

$res=mysql_query($sql);

$str5='';//保存的结果

while($row=mysql_fetch_array($res)) $str5.=$row[0].‘br';//可能你需要修改这一句,控制显示格式

mysql_free_result($res);

以后你就可以随时echo $str5了。

如何用php读取数据库第一条信息?

连接数据库我就不说了,直接给你SQL语句

$sql = "select * from table limit 1"

php读取mysql数据库表的前20条记录,然后分两行显示,每行10条记录,怎么编?

$result    = mysql_query('select * from mytable limit 0, 20');

$i       = 0;

while ($rs = mysql_fetch_array($result))

{

echo $rs['myfield'].' ';

$i++;

if ($i == 10)

{

echo 'br /';

}

}


新闻名称:php数据库取前面 php获取数据库的某条信息
网站路径:http://cdxtjz.cn/article/dddcsjs.html

其他资讯