//多條數(shù)據(jù)查詢 二維數(shù)組
10余年建站經(jīng)驗(yàn), 網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站設(shè)計(jì)客戶的見證與正確選擇。成都創(chuàng)新互聯(lián)提供完善的營銷型網(wǎng)頁建站明細(xì)報(bào)價(jià)表。后期開發(fā)更加便捷高效,我們致力于追求更美、更快、更規(guī)范。
while($row = mysql_fetch_array($sql)){
$arr[]= $row;
}
這樣的mysql_fetch_array用法是不對的,其引用的參數(shù)應(yīng)該是一個(gè)查詢結(jié)果而不是查詢條件!應(yīng)該結(jié)合mysql_query等查詢方法使用。
$con = mysql_connect("localhost","root","");//連接數(shù)據(jù)庫
mysql_select_db("btxiazai",$con);//選擇數(shù)據(jù)庫
mysql_query("set names utf8");
$sql = "select * from persons order by id desc limit 2";//獲取persons中的數(shù)據(jù),并按id倒敘排列,取其中兩條
$get = mysql_query($sql);//執(zhí)行sql
while($result = mysql_fetch_assoc($get)){//取回?cái)?shù)據(jù)
}
PHP查詢到的數(shù)據(jù)存放到數(shù)組里面,一般使用$arr[]=$row的方式實(shí)現(xiàn),$row是mysql_fetch_array獲得的一行數(shù)據(jù),本身是一個(gè)數(shù)組,執(zhí)行上面的語句之后,這一行會(huì)添加存放在額為數(shù)組$arr的最后。
典型的例子代碼是這樣的:
mysql_connect('127.0.0.1',?'root',?'123456');
$sql='select?*?from?test.tab';
if?($res=mysql_query($sql)){
while($row=mysql_fetch_array($res))?$result[]=$row;
mysql_free_resule($res);
}else?echo?"執(zhí)行SQL語句:$sqlbr\n錯(cuò)誤:".mysql_error();
echo?'查詢結(jié)果在下面的額為數(shù)組里面:pre';
print_r($result);
echo?'/pre';
該方法是根據(jù)一個(gè)條件查詢一個(gè)集合
$admin=Admin::model()-findAll($condition,$params);
$admin=Admin::model()-findAll("username=:name",array(":name"=$username));
$admin=Admin::model()-findAll(“username=:name and age=:age” , array(“:name”=$name, “age”=$age));
$admin=Admin::model()-findAll(“username like :name and age=:age” , array(“:name”=$name, “age”=$age));
$infoArr= NewsList::model()-findAll("status = '1' ORDER BY id DESC limit 10 ");
你這不是只查詢了一條,而是你只輸出了一條罷了
$sql="select?*?from?product?ORDER?BY?rand()?LIMIT?4?";
$mysql=mysql_query($sql);
while($row=mysql_fetch_assoc($mysql)){
//循環(huán)輸出記錄
print_r($row);
}
當(dāng)然,如果你的記錄不足4條,也沒法顯示出來
網(wǎng)頁題目:php查詢多條數(shù)據(jù) php實(shí)現(xiàn)多條件查詢
標(biāo)題路徑:http://muchs.cn/article28/docsdcp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT、外貿(mào)建站、網(wǎng)站導(dǎo)航、電子商務(wù)、小程序開發(fā)、網(wǎng)站改版
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)