php查詢mysql數(shù)據(jù)庫并將結(jié)果保存到數(shù)組的方法。具體分析如下:
創(chuàng)新互聯(lián)專業(yè)提供成都主機(jī)托管四川主機(jī)托管成都服務(wù)器托管四川服務(wù)器托管,支持按月付款!我們的承諾:貴族品質(zhì)、平民價格,機(jī)房位于中國電信/網(wǎng)通/移動機(jī)房,達(dá)州主機(jī)托管服務(wù)有保障!
主要用到了mysql_fetch_assoc函數(shù)
mysql_fetch_assoc語法如下:
?
1
array mysql_fetch_assoc (resource $Result_Set)
范例代碼如下:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
?php
$UserName = 'abc';
$Password = '1234';
$DbHandle = mysql_connect ('localhost', $UserName, $Password);
if (!$DbHandle) {
die 'No database connection could be established.';
}
$DBName = 'w3db;
if (!mysql_select_db ($DBName, $DbHandle)) {
die 'Database could not be selected.';
}
$Query = "SELECT ISBN, Title, Author FROM articles";
$articles = mysql_query ($Query, $DbHandle));
while ($Row = mysql_fetch_assoc ($articles)) {
echo "ISBN = $Row['ISBN']br /\n";
echo "Title = $Row['Title']br /\n";
echo "Author = $Row['Author']br /\n";
}
?
$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ù)組
while($row = mysql_fetch_assoc($res)){
$data[] = $row;
}
遍歷數(shù)據(jù)表,把相應(yīng)的數(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]['字段名稱'];
?
本文名稱:php讀取數(shù)據(jù)放入數(shù)組 php讀取數(shù)據(jù)放入數(shù)組中
鏈接分享:http://jinyejixie.com/article16/dddhhgg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站設(shè)計、自適應(yīng)網(wǎng)站、軟件開發(fā)、建站公司、靜態(tài)網(wǎng)站、外貿(mào)網(wǎng)站建設(shè)
聲明:本網(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)