MySQL Database Dump with all tables and data
Results 1 to 1 of 1

Thread: MySQL Database Dump with all tables and data

  1. #1
    phpMaster
    Join Date
    Jun 2005
    Location
    Europe
    Posts
    3,016

    MySQL Database Dump with all tables and data

    Sometimes you want to have a quick check of your database.

    This simple script will give a quick and dirty way to do a check.
    PHPmyadmin is of course a proper alternative.
    But the way I show here is quicker and very simple.

    My script uses MySQLi for connection.
    To run you need to configure MySQLi = host, user, pass.
    After this you are ready to run.

    You submit database name and click submit.
    After this you will see all tables and data in your selected database.
    If that mysql database does not exist, you will get an error.

    PHP Code:
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Dump Database</title>
    <style>
        body {background: #c7c7c7; font-size: 12px}
        table {background: #ffffee; border-collapse: collapse}
        th, td{border: 1px solid black}
    </style>
    </head>
    <body>
    <?php

    if (isset($_POST['database'])) {

        
    // MySQLi configure
        
    $host 'localhost';
        
    $user 'root';
        
    $pass '';
        
    // End config

        
    $base $_POST['database'];
        
    $db = new mysqli($host$user$pass);
        if (
    $db->connect_error)
            exit(
    'Connect Error('.$db->connect_errno.')'.$db->connect_error);
        if (
    $db->select_db($base)) {
            
    $result $db->query("SHOW TABLES FROM $base");
            
    $alltables = new stdClass;
            
    $i 0;
            while(
    $table $result->fetch_object()) {
                
    $i++;
                
    $alltables->$i $table->{"Tables_in_$base"};
            }

            echo 
    '<h3>Database: '.$base.'</h3>';

            foreach(
    $alltables as $table) {

                
    $result $db->query("SHOW COLUMNS FROM $table");
                
    $allcolumns = new stdClass;
                
    $i 0;
                while(
    $column $result->fetch_object()) {
                    
    $i++;
                    
    $allcolumns->$i $column->Field;
                }
                
    $numcol count((array)$allcolumns);
                echo 
    '<table>';
                echo 
    '<tr><th colspan="'.$numcol.'">'.$table.'</th></tr>';
                echo 
    '<tr>';
                foreach(
    $allcolumns as $col)
                    echo 
    '<th>'.$col.'</th>';
                echo 
    '</tr>';

                
    $result $db->query("SELECT * FROM $table");
                while(
    $row $result->fetch_object()) {
                    echo 
    '<tr>';
                    foreach(
    $row as $field)
                        echo 
    '<td>'.$field.'</td>';
                    echo 
    '</tr>';
                }
                echo 
    '</table><br>';    
            }
            
        } else {
            echo(
    '<i>'.$base.'</i> database does not exist.<br><br>');
        }
    }
    ?>

    <form method="post" accept-charset="UTF-8">
    MySQL database name:<br>
    <input type="text" name="database"><br>
    <input type="submit"></form>
    </body>
    </html>
    Apache-2.4.28 (Win64) / PHP-7.1.10 / MySQL-5.7.19 / phpMyAdmin-4.7.4

  2. #2
    Junior Member
    Join Date
    Feb 2018
    Posts
    1
    That's very useful! Thanks for sharing this thread, guys! I'll try to follow your directions, to have a quick check of my database.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •