php查詢數(shù)據(jù)庫存到數(shù)組,php查詢數(shù)據(jù)庫存到數(shù)組里

用php如何獲取數(shù)據(jù)庫中的數(shù)據(jù)并按條件存入數(shù)組中?

$conn=mysql_connect(localhost,root,root) or die("could not connect mysql");

創(chuàng)新互聯(lián)建站是工信部頒發(fā)資質(zhì)IDC服務(wù)器商,為用戶提供優(yōu)質(zhì)的成都西云數(shù)據(jù)中心服務(wù)

mysql_select_db(數(shù)據(jù)庫,$conn);

$query="SELECT * FROM 表";

$result=mysql_query($query);

while($array=mysql_fetch_array($result)){

arr[]=[$array[1],$array[2]],[$array[2],$array[3]],[$array[3],$array[4]];

}

如何把php 查詢結(jié)果存入數(shù)組

遍歷數(shù)據(jù)表,把相應(yīng)的數(shù)據(jù)放到數(shù)組中即可

例如:

?php

//定義一個(gè)數(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ù)庫的前5條數(shù)據(jù),用數(shù)組存起來

為了便于隨時(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ù)庫到數(shù)組 怎么弄

本文實(shí)例講述了php實(shí)現(xiàn)通用的從數(shù)據(jù)庫表讀取數(shù)據(jù)到數(shù)組的函數(shù)。分享給大家供大家參考。具體分析如下:

此函數(shù)不關(guān)心表結(jié)構(gòu),只需要指定表名、結(jié)構(gòu)和查詢條件既可以對(duì)表進(jìn)行通用查詢操作,非常實(shí)用。

function listmytablerows($table, $name, $field, $where, $textID) { / / Connect to the database and query execution connect (); $Sql = "select * from". $table. "". $where. "ORDER BY". $field; $Req = mysql_query($sql); $Res = mysql_num_rows($req); ? Select name = "?php echo $name; ?" id="?php echo $textID; ?" option value="" ____/ option ? Php / / We do a loop that will read the information for each record while ($data = mysql_fetch_array($res)) { / / We display the information from the current record ? Option value = "?php echo $data['id']; ?" ?php echo $data[$field]; ? / Option ? Php } ? / Select ? Php } ?

當(dāng)前名稱:php查詢數(shù)據(jù)庫存到數(shù)組,php查詢數(shù)據(jù)庫存到數(shù)組里
當(dāng)前URL:http://www.muchs.cn/article46/hssieg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)、標(biāo)簽優(yōu)化、網(wǎng)站營(yíng)銷ChatGPT、軟件開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

微信小程序開發(fā)