Monday, October 28, 2019

PHP script to export mysql database as sql file

PHP script to export mysql database as sql file:
-----------------------------------------------------------

<?php
//Enter your database information here and the name of the backup file
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

$user = 'root';
$pass = '';
$host = 'localhost';
$db = 'dbname';
$dbs = [];

$db_conn = mysqli_connect($host, $user, $pass, $db);

$result = mysqli_query( $db_conn, "SHOW DATABASES" );
while ( $row = mysqli_fetch_array($result) ) {
    //echo $row[0]."<br>";
    array_push($dbs, $row[0]);
}

foreach ( $dbs as $value ) {
    $dir = dirname(__FILE__) . '/backupDB' . '/' . $value . '.sql';
    echo "<h3>Backing up database to {$dir}</h3>";
    exec("D:\\xampp1\\mysql\\bin\\mysqldump --user={$user} --password={$pass} --host={$host} {$value} --result-file={$dir} 2>&1", $output);
    var_dump($output);
}


*** Change the above code as per your DB info and put the code file into htdocs and run it.

2 comments:

  1. Thank you for helping people get the information they need. Great stuff as usual. Keep up the great work!!!
    HTML templates

    ReplyDelete
  2. Thank you a bunch for this with all of us you actually realize what you are talking about! Bookmarked. Please also seek advice from my site =). We could have a hyperlink change contract between us! import records

    ReplyDelete

Suomen kieli (Imperfekti)

In Finnish, the imperfekti is the past tense that describes actions that happened in the past, like the English simple past (e.g., "I ...