查詢

mysql_tablename()函式—用法及示例

「 獲取指定資料庫連線中的表名列表 」


函式名:mysql_tablename()

適用版本:PHP 4, PHP 5

用法:mysql_tablename() 函式用於獲取指定資料庫連線中的表名列表。

語法:array mysql_tablename ( resource $result [, int $i = 0 ] )

引數:

  • result:必需,表示一個 MySQL 查詢結果資源識別符號。
  • i:可選,表示結果集中表名的索引。預設為 0,表示返回第一個表名。

返回值:返回一個包含表名的陣列,如果沒有表名則返回 FALSE。

示例: 假設我們已經連線到了一個 MySQL 資料庫,並執行了一個查詢,查詢結果包含了多個表。

<?php
$db = mysql_connect('localhost', 'username', 'password');
if (!$db) {
    die('Could not connect: ' . mysql_error());
}

mysql_select_db('database_name', $db);

$result = mysql_query("SHOW TABLES", $db);

if (!$result) {
    die('Invalid query: ' . mysql_error());
}

$tables = mysql_tablename($result); // 獲取表名列表

if (!$tables) {
    echo 'No tables found.';
} else {
    echo 'Tables in the database:<br>';
    foreach ($tables as $table) {
        echo $table . '<br>';
    }
}

mysql_close($db);
?>

輸出結果:

Tables in the database:
table1
table2
table3

注意:

  • mysql_tablename() 函式已經在 PHP 7.0.0 版本中被廢棄,不再推薦使用。推薦使用 mysqli 或 PDO 擴充套件來連線和操作 MySQL 資料庫。
補充糾錯
上一個函式: mysql_thread_id()函式
下一個函式: mysql_stat()函式
熱門PHP函式
分享連結