Hongmu Notes
Home Language Notes PHP – Retrieve all tables from a database
Language Notes PHP PHP and mysql

PHP – Retrieve all tables from a database

PHP – Retrieve all tables from a database

To retrieve all tables from a database using PHP, you can use an SQL query to select all table names from a specific database and store the results in an array. The following example demonstrates how to query all tables in a database using PHP and MySQL:

<?php
// 定义数据库连接信息
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database_name";

// 创建数据库连接
$conn = new mysqli($servername, $username, $password, $dbname);

// 检查连接是否成功
if ($conn->connect_error) {
    die("连接失败:" . $conn->connect_error);
}

// 查询数据库中所有表名
$sql = "SHOW TABLES";
$result = $conn->query($sql);

// 将表名存储在数组中
$tables = array();
if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
        $tables[] = $row['Tables_in_'.$dbname];
    }
}

// 输出表名
echo "该数据库中有以下表:";
foreach ($tables as $table) {
    echo $table . "<br>";
}

// 关闭数据库连接
$conn->close();
?>

In the above code, we first define the database connection details—including the host name, username, password, and database name. Then, we establish a database connection and use the SQL statement "SHOW TABLES" to query all table names in the database. The query results are stored in an array, and all table names are then printed out.

Finally, we close the database connection to release resources.

function encapsulation

 隐藏内容:付费阅读
function getAllTables($servername, $username, $password, $dbname) {
    // 创建数据库连接
    $conn = new mysqli($servername, $username, $password, $dbname);

    // 检查连接是否成功
    if ($conn->connect_error) {
        die("连接失败:" . $conn->connect_error);
    }

    // 查询数据库中所有表名
    $sql = "SHOW TABLES";
    $result = $conn->query($sql);

    // 将表名存储在数组中
    $tables = array();
    if ($result->num_rows > 0) {
        while ($row = $result->fetch_assoc()) {
            $tables[] = $row['Tables_in_'.$dbname];
        }
    }

    // 关闭数据库连接
    $conn->close();

    return $tables;
}

This function accepts four parameters: host name, username, password, and database name. It uses these parameters to establish a database connection and executes the SQL statement "SHOW TABLES" to query all table names in the database. The query results are stored in an array, and the database connection is closed at the end of the function. Finally, the function returns an array containing all the table names.

You can use this function as follows:

// 定义数据库连接信息
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database_name";

// 调用函数获取所有表名
$tables = getAllTables($servername, $username, $password, $dbname);

// 输出表名
echo "该数据库中有以下表:";
foreach ($tables as $table) {
    echo $table . "<br>";
}
微信赞赏

WeChat

支付宝赞赏

Alipay

✍️ Author: Hong Mu

webmaster · Thanks for reading, stay tuned for more exciting content

Author homepage View home page →

Related articles

PHP cast type

PHP cast type Language Notes PHP PHP collection PHP and mysql

Get the data type 1. If you want to check the value and type of an expression, use var_dump(). 2. If you just want to get an easy-to-read type expression for debugging, use gettype(). 3. To check a certain type, do not use gettype(), but use the is_type() function. Converting Strings to Numbers When a string is evaluated as a number, the result is determined according to the following rules...
👁 232
PHP special character escaping and restoration

PHP special character escaping and restoration Language Notes PHP PHP collection PHP and mysql

Escape character is a special character constant. Escape characters are backslashed " & quot; At the beginning, followed by one or more characters. The escaped character has a specific meaning, which is different from the original meaning of the character, so it is called "escaped" character. The use of escape characters 1: turn ordinary characters into special purposes, such as back key and enter key. 2. Used to convert a character with special meaning back to its original meaning. 3. Before data is written into the database, escape characters (function …
👁 180
mysqli in PHP

mysqli in PHP Language Notes PHP PHP collection PHP and mysql

The `mysqli_num_rows()` function is exclusively used with `SELECT` query methods, whereas the `mysqli_affected_rows()` function returns the number of rows affected by the previous SQL statement across the entire database; this function is primarily used with `INSERT`, `UPDATE`, and `DELETE` operations.
👁 146

Recommended reading

(Adaptive Mobile Version) Advertising Agency Website Template – Download Source Code for Advertising Design Agency Websites – 1051

(Adaptive Mobile Version) Advertising Agency Website Template – Download Source Code for Advertising Design Agency Websites – 1051 Practical Collection pbootcms Template

A PbootCMS website template designed for advertising and cultural agencies as well as advertising design firms, supporting mobile access. Its creative and visually appealing design makes it ideal for these agencies to showcase their brand strategy, design works, and case studies. This template helps advertising agencies demonstrate their professional expertise online and attract brand clients. Template Display | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5.c...
👁 66
Underfloor Heating Equipment Website Template 0871

Underfloor Heating Equipment Website Template 0871 Practical Collection Yiyou template

This EyouCMS template is designed for the underfloor heating equipment industry, featuring a professional yet comfortable design style ideal for showcasing underfloor heating products, heating systems, and project cases. It enables HVAC equipment manufacturers to showcase their products online and attract both residential and commercial clients. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS | EyouCMS (EyouC...
👁 36
(PC+WAP) Marketing-oriented eco-friendly, energy-saving smart air purifier – pbootCMS template; Download source code for a green, energy-efficient, and environmentally friendly corporate website – 0102

(PC+WAP) Marketing-oriented eco-friendly, energy-saving smart air purifier – pbootCMS template; Download source code for a green, energy-efficient, and environmentally friendly corporate website – 0102 Practical Collection pbootcms Template

This PbootCMS template features a green, energy-efficient design, ideal for smart air purifier manufacturers and environmentally friendly, energy-saving enterprises, and is compatible with both PC and WAP devices. Its modern, minimalist design effectively showcases smart purification products and technical solutions. It is an excellent choice for eco-tech companies looking to build a professional brand website and enhance their brand influence. Template Display | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin...
👁 37
(Adaptive mobile version) Furniture manufacturing company website template – PBootCMS furniture website source code download (1052)

(Adaptive mobile version) Furniture manufacturing company website template – PBootCMS furniture website source code download (1052) Practical Collection pbootcms Template

A PbootCMS website template designed for furniture manufacturing companies, compatible with both PC and WAP devices. Its modern and practical design is ideal for showcasing furniture products, production capabilities, and brand stories. This template helps furniture businesses present their brands online and attract distributors and consumers. Template Overview | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5.cn | Related Articles: Pbo...
👁 57
Responsive interior design and building materials flooring website template – 0872

Responsive interior design and building materials flooring website template – 0872 Practical Collection Yiyou template

An eyouCMS responsive website template designed for flooring product manufacturers and construction material companies. Its fashionable, modern design effectively showcases flooring products, installation outcomes, and spatial applications. This template helps flooring brands enhance their online presence and attract both construction firms and consumers. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for eyouCMS | eyouCMS (...
👁 35
(PC + WAP) Marketing-oriented eco-friendly shell powder ecological coating – pbootCMS website template; Download source code for cyan paint coating website – 0103

(PC + WAP) Marketing-oriented eco-friendly shell powder ecological coating – pbootCMS website template; Download source code for cyan paint coating website – 0103 Practical Collection pbootcms Template

A PbootCMS marketing website template for environmental protection shell powder ecological coating and paint and coating industry, using cyan style, support PC and WAP end. The design style is fresh and environmentally friendly, and can display ecological coating products and coating engineering cases. Help paint enterprises to promote green building materials online, to attract more home decoration and engineering customers. Template display, installation instructions website background: / admin.php Account: admin password: ad...
👁 46