新聞中心
php查詢(xún)MYSQL數(shù)據(jù)庫(kù) 前5條內(nèi)容 并顯示出來(lái)
?php
西城網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián)公司,西城網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為西城數(shù)千家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)網(wǎng)站制作要多少錢(qián),請(qǐng)找那個(gè)售后服務(wù)好的西城做網(wǎng)站的公司定做!
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查詢(xún)數(shù)據(jù)庫(kù)的前5條數(shù)據(jù),用數(shù)組存起來(lái)
為了便于隨時(shí)echo,存為字符串最好,一般的代碼如下:
$sql='select * from xxx order by xxid desc limit 5';//limit 5表示只取5個(gè),order by xxxid desc表示按xxxid降序排列,可以顯示最新的5個(gè)
$res=mysql_query($sql);
$str5='';//保存的結(jié)果
while($row=mysql_fetch_array($res)) $str5.=$row[0].‘br';//可能你需要修改這一句,控制顯示格式
mysql_free_result($res);
以后你就可以隨時(shí)echo $str5了。
如何用php讀取數(shù)據(jù)庫(kù)第一條信息?
連接數(shù)據(jù)庫(kù)我就不說(shuō)了,直接給你SQL語(yǔ)句
$sql = "select * from table limit 1"
php讀取mysql數(shù)據(jù)庫(kù)表的前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數(shù)據(jù)庫(kù)取前面 php獲取數(shù)據(jù)庫(kù)的某條信息
轉(zhuǎn)載來(lái)源:http://www.ef60e0e.cn/article/dddcsjs.html