0
我有這樣的代碼:多維數組迭代 - 我的眼睛已經越過
try {
$this->_connect = new PDO(DB_TYPE.':host='.DB_HOST.';dbname='.DB_NAME, DB_USER, DB_PASS);
$this->_connect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $this->_connect->query('SHOW TABLES FROM '.DB_NAME, PDO::FETCH_ASSOC);
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
foreach ($row as $key => $value) {
$this->_schema[$value] = "";
}
}
foreach ($this->_schema as $key => $value) {
$stmt = $this->_connect->query('SELECT COLUMN_NAME, DATA_TYPE, COLUMN_DEFAULT, CHARACTER_MAXIMUM_LENGTH '.
"FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = '$key'");
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$this->_schema[$key][] = $row;
}
}
foreach ($this->_schema as $key => $value) {
$colData = null;
foreach ($this->_schema[$key] as $keyTwo => $valueTwo) {
foreach ($this->_schema[$key][$keyTwo] as $keyThree => $valueThree) {
$colData[][$keyThree] = $valueThree;
}
}
$this->$key = new Table($colData);
}
}
catch(PDOException $e) {
echo $e->getMessage();
}
if (isset($stmt))
$stmt->closeCursor();
}
}
Class Table {
public $select;
public $update;
public $delete;
public $insert;
public $replace;
private $_columns;
public function __construct($colData) {
$this->select = new Select;
$this->update = new Update;
$this->delete = new Delete;
$this->insert = new Insert;
$this->replace = new Replace;
$this->_columns = $colData;
var_dump($this->_columns[0]);
}
它試圖抓住從數據庫的整個模式。當我var_dump時,$ this - > _ schema看起來不錯,所以我認爲問題是在完成之後。我試圖迭代數據庫,獲取每個表的元數據並將數據存儲在數組中。然後,我遍歷數組爲每個表創建一個新的Table對象,並向它發送一個我提取的列數據數組。我一直在尋找這個代碼,以獲得今天更好的一部分,並且無法弄清楚我錯誤的地方。
當我VAR在最後一行有傾倒,它轉儲這樣的:
array(1) { ["COLUMN_NAME"]=> string(4) "type" } array(1) { ["COLUMN_NAME"]=> string(8) "username" } array(1) { ["COLUMN_NAME"]=> string(2) "id" }
哪項是錯誤的 - 它是表1中的列名1列;表2第1欄;和表3第1列。它應該是表1第1列的名稱,數據類型,默認值和最大長度。
任何人都可以看到我出錯的地方或者提出了更好的方法來解決這個問題嗎?
我的眼睛也劃過了...... – JBES