简体   繁体   中英

List all the tables in your MySQL database using PHP

I have a hard time trying to list all the tables in my database.


I tried

<?php 

//Configuration
$dbname      = 'local';
$user        = 'root';
$host        = '127.0.0.1';
$pass        = '';
$date        = date('Y-m-d');
$export_type = 'mysql'; // option : mysql | psql
$file_name   = $date.'-portal';
$file_path   = $file_name;



// Create connection
$conn = mysqli_connect($host, $user, $pass);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

echo "Connected successfully";

$sql = "SHOW TABLES FROM $dbname";
$res = mysqli_query($conn, $sql);

if($res != false){

    echo "Connected successfully";
    $FILE = fopen("output.csv", "w");

    $table = array();
    while($row = mysql_fetch_array($res)){
       $table[] = $row['0'];
    }

    foreach($tables as $table) {
        $columns = array();
        $res = mysqli_query($conn, "SHOW COLUMNS FROM $table");

        while($row = mysql_fetch_array($res, MYSQL_NUM)) {
            $columns[] = "$row[0]";
        }
        fwrite($FILE, implode(",", $columns)); fwrite("\n");
        $resTable = mysqli_query($conn, "SELECT * FROM $table");

        while($row = mysql_fetch_array($resTable, MYSQL_NUM)) {
            fwrite($FILE, implode(",", $row)); fwrite("\n");
        }
    }
}else{
    die(mysql_error());
}

?>

Result

if($res != false){
//.. everything in here never get executed
}

`$res` kept returning `false`. 

What did I do wrong that could have lead to this ?

You need to pass through your database in the connection script.

Like so:

$conn = mysqli_connect($host, $username, $pass, $dbname);

Then, when you want to pull rows from a table, you do it like this:

mysqli_query($conn, "SELECT rows FROM table");

One of the reasons this wasn't working for you was because you weren't passing through your database name through the connection. Also, rather than doing the above query, you selected a table from a database; rather than a row from a table.

Also, I noticed that you're using the mysql_* error output on the last line.

Use db in your connection

 mysqli_connect($host, $user, $pass, $dbname);

And use query like this

 $sql = "SHOW TABLES";

You can always execute the query:

Show tables;

After you selected database.

By the way you also can execute:

Show databases;

To list all of the databases your current user has permission to view.

Here is a working version

Changes:

  • Added $dbname to mysqli_connect function
  • Added the backtick ` char between the table names, to avoid errors with reserved keyword from MySQL
  • Changed mysql_ functions to mysqli_
  • Close the file
  • Close the connection

Here is the code

NOTE: sorry I don't why, but when I pasted the code in the answer, all de code identation was messed up, even trying to indent it properly, I wasted like 10 minutes :(

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM