本文實(shí)例講述了php實(shí)現(xiàn)通用的從數(shù)據(jù)庫表讀取數(shù)據(jù)到數(shù)組的函數(shù)。分享給大家供大家參考。具體分析如下:
創(chuàng)新互聯(lián)是一家專注于成都網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)與策劃設(shè)計(jì),靜安網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)十年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:靜安等地區(qū)。靜安做網(wǎng)站價(jià)格咨詢:18982081108
此函數(shù)不關(guān)心表結(jié)構(gòu),只需要指定表名、結(jié)構(gòu)和查詢條件既可以對表進(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 } ?
PHP有自帶的高性能函數(shù) var_export
conn.php
?php
$dbconfig = array (
'host'='127.0.0.1',
'name'='root',
'password'='123456',
?
b.php
?php
// 讀取配置
include 'conn.php';
echo $dbconfig['host'];
// 修改配置
$dbconfig['host'] = 'xxx.xxx.xxx.xxx';
file_put_contents('conn.php', "?php\n$dbconfig = " . var_export($dbconfig) . "\n?");
// 再讀取配置
include 'conn.php';
echo $dbconfig['host'];
?
參考連接:
數(shù)據(jù)庫提到的數(shù)據(jù)一般是資源類型的,要逐一讀出,添加到數(shù)組
while($row = mysql_fetch_assoc($res)){
$data[] = $row;
}
遍歷數(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]['字段名稱'];
?