首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 綜合知識

PHP 取得 MySQL 所有資料表名稱(SHOW TABLES)

     編輯:PHP 綜合知識

標準做法

<?php
$server = 'localhost';
$user = 'root';
$pass = '';
$dbname = 'dayanmei_com';

$conn = mysql_connect($server, $user, $pass);
if (!$conn) die("資料庫連線失敗!");

mysql_select_db($dbname) or die("選擇資料庫失敗!");

$result = mysql_query("SHOW TABLES");
while ($row = mysql_fetch_array($result)) {
    echo $row[0] . "
"; } mysql_free_result($result); ?>

⚠️ 棄用警告

mysql_list_tables() 已被移除(PHP 4.0 起棄用,PHP 7.0 移除),請勿使用。

改用 SHOW TABLES 或資訊庫查詢。

現代 PDO 寫法

<?php
$pdo = new PDO('mysql:host=localhost;dbname=dayanmei_com', 'root', '');
$stmt = $pdo->query("SHOW TABLES");
while ($row = $stmt->fetch(PDO::FETCH_NUM)) {
    echo $row[0] . "
"; } // 或查詢 information_schema $stmt = $pdo->query("SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_SCHEMA = 'dayanmei_com'"); while ($row = $stmt->fetch()) { echo $row['TABLE_NAME'] . "
"; } ?>

mysqli 寫法

<?php
$mysqli = new mysqli('localhost', 'root', '', 'dayanmei_com');
$result = $mysqli->query("SHOW TABLES");
while ($row = $result->fetch_row()) {
    echo $row[0] . "
"; } $result->free(); $mysqli->close(); ?>
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved