我想“自动”将我的所有数据库数据加载到$db[]
和/或$_SESSION['db']
. 因此,我只需加载此代码一次,而不是在整个网站上加载零碎。
我能够查询元数据和原始数据,我的问题是创建数组。
假设我有一个temp
包含列的表,name
并且age
我希望能够执行以下操作: -echo $db['temp']['name'] . "'s age is: " . $db['temp']['age'];
显然我有很多包含很多列的表。这只是一个例子
<?php
$dbn = "db_name";
$dbu = "db_user";
$dbp = "db_password";
$hst = 'db_host';
$dbc = mysql_connect($hst,$dbu,$dbp);
$sql_metadata =
"SELECT `COLUMNS`.`TABLE_NAME` as 'table' , `COLUMNS`.`COLUMN_NAME` as 'column'
FROM `information_schema`.COLUMNS
WHERE `COLUMNS`.`TABLE_SCHEMA` = '$dbn'
ORDER BY TABLE_NAME ASC , `COLUMNS`.`COLUMN_NAME` ASC";
$dbq = mysql_query($sql_metadata, $dbc);
while( $meta = mysql_fetch_array($dbq) )
{
$sql_rawdata = "SELECT `$meta['column']` FROM `$dbn`.`$meta['table']`";
$dbq2 = mysql_query($sql_rawdata);
foreach( ($raw = mysql_fetch_array($dbq2)) as $value) )
{
$db = array
(
$meta['table'] => array( $meta['column'] => $value)
);
}
}
print_r($db);
?>