0

I have been seriously searching for an SQL code to backup my database. I use xampp as my local server and also phpmyadmin. I simply want to do something like:

    <?php

          $conn = mysqli_connect("localhost", "root", " ", "products");
          if(!$conn){
             die("Unable to connect ".mysql_error());
          }else{
              $backup = "BACKUP DATABASE products";
              $backup_query = mysqli_query($conn, $backup)
          }
   ?>

How do I backup my database and output it in .sql format on the local computer and possibly upload to recover a damaged database? Thank you so much!

Thomas Rollet
  • 1,573
  • 4
  • 19
  • 33
neltino
  • 1
  • 2
  • 1
    please see this https://stackoverflow.com/questions/22195493/export-mysql-database-using-php-only – D Coder Nov 23 '17 at 09:55

1 Answers1

0

You can use the shell statement "mysqldump" and call it with php like:

<?php
$dbhost = 'localhost';
$dbuser = 'username';
$dbpassword = 'password';
$dbname = 'datenbankname';
$dumpfile = 'backups/' . $dbname . '_' . date("Y-m-d_H-i-s") . '.sql.gz';

passthru("mysqldump --user=$dbuser --password=$dbpassword --host=$dbhost $dbname | gzip -c  > $dumpfile");
echo "-- Dump finished -- ";
echo $dumpfile;
episch
  • 388
  • 2
  • 19