2016-05-30 92 views
0

我正在嘗試使用函數mysqli_fetch_field()來獲取數據庫中每個表的名稱。但是,當我嘗試使用$fieldInfo->table輸出表名稱時,我得到了重複項。我怎樣才能從每個表中只選擇1列,以便$fieldInfo->table不是針對每個表的每列調用的?PHP/SQL - 如何獲取數據庫中表的名稱?

當前SQL:

$sql = "SELECT * from administrators, bookings, customers, rooms"; 
$results = mysqli_query($conn, $sql) 
or die ('Problem with query' . mysqli_error($conn)); 

我的代碼來顯示在單選按鈕表名:

<?php 
while ($fieldInfo = mysqli_fetch_field($results)) { 
?> 
<input type="radio" name="tableNames" value="<?php echo $fieldInfo->table; ?>"> <?php echo $fieldInfo->table ?> <br> 
<?php } ?> 
+0

'資源mysql_list_tables(字符串$數據庫[,資源$ link_identifier = NULL])',[function.mysql一覽tables.php](http://php.net/manual/en /function.mysql-list-tables.php) –

+1

@FrayneKonok'mysqli'和'mysql'之間有區別... – arkascha

+0

'SELECT 1 from administrative,bookings,customers,rooms'? – arkascha

回答

0

我加入2-臨時表名保持器和由IF條件是僅輸出單選按鈕一旦兩名臨時姓名持有人不同。

<?php 
    $tempName2 = ""; 
    while ($fieldInfo = mysqli_fetch_field($results)) { 
     $tempName = $fieldInfo->table; 
     if ($tempName != $tempName2) { 
      $tempName2 = $tempName; 
?> 
    <input type="radio" name="tableNames" value="<?php echo $tempName; ?>" > <?php echo $tempName ?> <br> 
    <?php } 
} ?> 
0
<?php 
$query='SHOW TABLES FROM DB_NAME'; 
$results=mysqli_query($conn,$query); 
while ($fieldInfo = mysqli_fetch_array($results)) { ?> 
    <input type="radio" name="tableNames" value="<?php echo $fieldInfo[0]; ?>"> <?php echo $fieldInfo[0]; ?> <br> 
<?php } ?> 
相關問題