2016-08-04 28 views
1

各テーブルのすべてのフィールドを表示し、それらをマルチレベル配列に割り当てようとしていますが、 '非オブジェクト上のメンバ関数fetch_assoc()を呼び出します。' ...foreachループから列を表示する

$sql_tables = "SHOW TABLES;"; 
$result = $mysql->query($sql_tables); 
if ($result->num_rows > 0) { 
    while($row_tables = $result->fetch_assoc()) { 
      $alltables_array[] = $row_tables["Tables_in_dbname"]; 
    } 
} 

foreach ($alltables_array as $table) { 
    $sql_fields = "SHOW COLUMNS FROM " . $table . ";"; 
    $result_fields= $mysql->query($sql_fields); 
    while($row_fields = $result_fields->fetch_assoc()) { /* ERROR ON THIS LINE */ 
      $allfields_array[$table ][] = $row_fields['Field']; 
    } 
}  

ありがとう! $row_tables以来

答えて

2

は、現在のデータベース名をもたらす:

$row_tables["Tables_in_dbname"]; // this is incorrect (unless your current dbname is really dbname) 
      //^undefined index 

だから動的インデックスを追加:

if ($result->num_rows > 0) { 
    while($row_tables = $result->fetch_row()) { 
      $alltables_array[] = $row_tables[0]; 
    } 
} 

その後のポイント:私は代わりに使用->fetch_row()をお勧めしたい

$dbname = 'test'; 
if ($result->num_rows > 0) { 
    while($row_tables = $result->fetch_assoc()) { 
      $alltables_array[] = $row_tables['Tables_in_' . $dbname]; 
    } 
} 

をインデックスをゼロにする。連想インデックスに動的変数を追加する必要はありません。

追記:

$alltables_array[] = $row_tables[key($row_tables)]; // not good though, it'll invoke key() every iteration. 
+0

追記:バンドエイドの解決策になることができます ';クエリ文あなたの答えのための – Ghost

+0

おかげで不要で'セミコロン。申し訳ありませんが、あまり正確ではないかもしれませんが、while($ row_fields = $ result_fields ... line'でエラーが表示されます。質問を編集します。データベース関数... – codeispoetry

+0

@codeispoetryはい私は、コードのバグをテストした、エラーが連鎖している、起点がテーブルのフェッチが間違っている、正しいテーブルをフェッチ、列が正しく正しくフェッチされます – Ghost

関連する問題