2016-06-13 63 views
0

我試圖把MySQL數據庫中的表格放在使用PHP的HTML頁面中。
我是PHP初學者,面臨mysqli_query函數的問題。如何顯示數據庫中的表格?

這是我的PHP代碼:

// connect to the db 
$host = 'localhost'; 
$user = 'root'; 
$pass = ''; 
$db = 'testdb'; 
$connection = mysqli_connect($host, $user, $pass, $db) or die("cannot connect to db"); 

// show the tables 
$result = mysqli_query($connection, 'SHOW TABLES') or die ('cannot show tables'); 
while($tableName = mysqli_fetch_row($result)) { 
    $table = $tableName[0]; 
    echo '<h3>', $table, '</h3>'; 
    $result2 = mysqli_query($table, 'SHOW COLUMNS FROM') or die("cannot show columns"); 
    if(mysqli_num_rows($result2)) { 
    echo '<table cellpadding = "0" cellspacing = "0" class "db-table">'; 
    echo '<tr><th>Field</th><th>Type</th><th>Null</th><th>Key</th><th>Default</th><th>Extra</th></tr>'; 
    while($row2 = mysqli_fetch_row($result2)) { 
     echo '<tr>'; 
     foreach ($row2 as $key=>$value) { 
     echo '<td>',$value, '</td>'; 
     } 
     echo '</tr>'; 
    } 
    echo '</table><br />'; 
    } 
} 

不幸的是我得到這個錯誤:

Warning: mysqli_query() expects parameter 1 to be mysqli, string given in C:\xampp\htdocs\test\showtables.php on line 26, cannot show columns.

我也試圖與mysql_query功能,但我面臨的另一個錯誤,我後來我改變它回到mysqli_query函數。

任何幫助將不勝感激,謝謝你提前。

+3

請閱讀'mysqli'手冊。特別是功能參數的一部分。 –

+2

[Mysqli \ _Query警告:mysqli \ _query()的可能重複需要參數1爲mysqli](http://stackoverflow.com/questions/10160664/mysqli-query-warning-mysqli-query-expects-parameter-1 -o-mysqli) –

+0

按照你的第一個用法,'mysqli_query($ connection,'..這裏是手冊:http://php.net/manual/en/mysqli.query.php。具體來說:'mixed mysqli_query mysqli的$鏈接,串$查詢[摘要$ resultmode = MYSQLI_STORE_RESULT])'。 – chris85

回答

2

這個代碼可以顯示所有的表和表中的行。我嘗試它的工作原理

<?PHP 
    $host = 'localhost'; 
    $user = 'root'; 
    $pass = ''; 
    $db = 'testdb'; 

    $mysqli = new mysqli($host, $user, $pass, $db); 

    //show tables 
    $result = $mysqli->query("SHOW TABLES from testdb"); 
    //print_r($result); 
    while($tableName = mysqli_fetch_row($result)) 
    { 
     $table = $tableName[0]; 
     echo '<h3>' ,$table, '</h3>'; 
     $result2 = $mysqli->query("SHOW COLUMNS from ".$table.""); //$result2 = mysqli_query($table, 'SHOW COLUMNS FROM') or die("cannot show columns"); 
     if(mysqli_num_rows($result2)) 
     { 
      echo '<table cellpadding = "0" cellspacing = "0" class "db-table">'; 
      echo '<tr><th>Field</th><th>Type</th><th>Null</th><th>Key</th><th>Default</th><th>Extra</th></tr>'; 
      while($row2 = mysqli_fetch_row($result2)) 
      { 
       echo '<tr>'; 
       foreach ($row2 as $key=>$value) 
       { 
        echo '<td>',$value, '</td>'; 
       } 
       echo '</tr>'; 
      } 
      echo '</table><br />'; 
     } 
    } 
?> 

樣本輸出:

enter image description here

2

在第二次調用mysqli_query函數時,您傳遞的是表名,而不是連接。嘗試是這樣的:

$result2 = mysqli_query($connection, "SHOW COLUMNS FROM $table") or die("cannot show columns");

http://php.net/manual/en/mysqli.query.php

+0

我試過了,但現在卻diplayed 「死信息」:無法顯示列...有些東西仍然是錯誤的 – Caroso

+0

請確保您要麼使用雙引號'($ connection,「SHOW COLUMNS FROM $ table」)'或者連接'($ connection,'SHOW COLUMNS FROM'。$ table)'。請參閱[PHP中的單引號和雙引號之間的區別](http:// st ackoverflow.com/questions/3446216/what-is-the-difference-between-single-quoted-and-double-quoted-strings-in-php) – user6459501

相關問題