PHP查詢到的數(shù)據(jù)存放到數(shù)組里面,一般使用$arr[]=$row的方式實現(xiàn),$row是mysql_fetch_array獲得的一行數(shù)據(jù),本身是一個數(shù)組,執(zhí)行上面的語句之后,這一行會添加存放在額為數(shù)組$arr的最后。
創(chuàng)新互聯(lián)專注于本溪企業(yè)網(wǎng)站建設,響應式網(wǎng)站開發(fā),購物商城網(wǎng)站建設。本溪網(wǎng)站建設公司,為本溪等地區(qū)提供建站服務。全流程按需求定制制作,專業(yè)設計,全程項目跟蹤,創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務
典型的例子代碼是這樣的:
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錯誤:".mysql_error();
echo?'查詢結(jié)果在下面的額為數(shù)組里面:pre';
print_r($result);
echo?'/pre';
$str='{"data":[{"name":"111","img":"748.jpg","dz":"uang","sz":"22"},{"name":"222","img":"888.jpg","dz":"ngzhu","sz":"13"},{"name":"333","img":"999.jpg","dz":"ve","sz":"27"}]}';
$json=json_decode($str,true);
echo?$json['data'][0]['name']."br?/";
echo?$json['data'][0]['img']."br?/";
echo?$json['data'][0]['dz']."br?/";
echo?$json['data'][0]['sz']."br?/";
//或者
$json=json_decode($str);
echo?$json-data[0]-name."br?/";
echo?$json-data[0]-img."br?/";
echo?$json-data[0]-dz."br?/";
echo?$json-data[0]-sz."br?/";
遍歷數(shù)據(jù)表,把相應的數(shù)據(jù)放到數(shù)組中即可
例如:
?php
//定義一個數(shù)組,用于保存讀取到的數(shù)據(jù)
$contents
=
array();
$query
=
mysql_query("select
*
from
table");
//遍歷數(shù)據(jù)表
while($array
=
mysql_fetch_array($query)){
$contents[]
=
$array;
}
print_r($contents);
//然后循環(huán)數(shù)組,或者通過鍵名使用數(shù)組
foreach($contents
as
$value){
print_r($value);
}
echo
$contents[0]['字段名稱'];
?
數(shù)據(jù)庫提到的數(shù)據(jù)一般是資源類型的,要逐一讀出,添加到數(shù)組
while($row = mysql_fetch_assoc($res)){
$data[] = $row;
}
網(wǎng)頁標題:php將數(shù)據(jù)放入數(shù)組中 php怎么把數(shù)組變成字符串
標題來源:http://www.muchs.cn/article4/ddcedoe.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供Google、網(wǎng)站改版、網(wǎng)站設計、微信公眾號、微信小程序、網(wǎng)站營銷
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)