Devnote

Web Development Blog Company, Services India

PHP cURL error 60: SSL certificate problem - Windows & Linux Fix
PHP cURL error 60: SSL certificate problem – Windows & Linux Fix
February 11, 2026
Fix localhost refused to connect After Installing XAMPP / WAMP
Fix “localhost refused to connect” After Installing XAMPP / WAMP
February 9, 2026
Composer Error Memory exhausted When Installing Packages - Real Fix
Composer Error: Memory exhausted When Installing Packages – Real Fix
February 6, 2026
MySQL 8 Error Unknown collation: utf8mb4_0900_ai_ci - Safe Migration Guide
MySQL 8 Error Unknown collation: utf8mb4_0900_ai_ci – Safe Migration Guide
February 4, 2026
WordPress Critical Error When Editing with Gutenberg - Root Cause + Fix
WordPress Critical Error When Editing with Gutenberg – Root Cause + Fix
February 2, 2026
Devnote

Type and hit Enter to search

  • Home
  • Laravel
    • Auth
    • Migration
    • DATATABLE
    • Yajra
  • wordpress
    • plugin
    • WPBakery
    • woocommerce
  • PHP
    • MYSQL
    • Ajax
  • Blog
  • Informational
    • About us
    • Privacy Policy
    • Disclaimer
    • Terms and Conditions
  • Contact US
Devnote
  • Home
  • Laravel
    • Auth
    • Migration
    • DATATABLE
    • Yajra
  • wordpress
    • plugin
    • WPBakery
    • woocommerce
  • PHP
    • MYSQL
    • Ajax
  • Blog
  • Informational
    • About us
    • Privacy Policy
    • Disclaimer
    • Terms and Conditions
  • Contact US
Export MySQL database to SQL

How to export MySQL database to SQL file using PHP

Image
Devnote team
March 3, 2025
One Min Read
5.2K Views
4 Comments

This tutorial is for How to export MySQL database to SQL file using PHP. This is the Best way to Export MySQL Database. Simple you can download the database in your hostname, username, password, and database name.

Sometimes, we need to create our own custom PHP code to import or export databases. So, in this tutorial, we will discuss how to export MySQL database to SQL file using PHP. Import and Export is always good practice to take a regular backup of your database. There are three ways to take backup of your MySQL database.

SQL Command through PHP.
MySQL binary mysqldump through PHP.
phpMyAdmin user interface.

<?php

function backupDatabaseAllTables($dbhost,$dbusername,$dbpassword,$dbname,$tables = '*'){
    $db = new mysqli($dbhost, $dbusername, $dbpassword, $dbname); 

    if($tables == '*') { 
        $tables = array();
        $result = $db->query("SHOW TABLES");
        while($row = $result->fetch_row()) { 
            $tables[] = $row[0];
        }
    } else { 
        $tables = is_array($tables)?$tables:explode(',',$tables);
    }

    $return = '';

    foreach($tables as $table){
        $result = $db->query("SELECT * FROM $table");
        $numColumns = $result->field_count;

        /* $return .= "DROP TABLE $table;"; */
        $result2 = $db->query("SHOW CREATE TABLE $table");
        $row2 = $result2->fetch_row();

        $return .= "\n\n".$row2[1].";\n\n";

        for($i = 0; $i < $numColumns; $i++) { 
            while($row = $result->fetch_row()) { 
                $return .= "INSERT INTO $table VALUES(";
                for($j=0; $j < $numColumns; $j++) { 
                    $row[$j] = addslashes($row[$j]);
                    $row[$j] = $row[$j];
                    if (isset($row[$j])) { 
                        $return .= '"'.$row[$j].'"' ;
                    } else { 
                        $return .= '""';
                    }
                    if ($j < ($numColumns-1)) {
                        $return.= ',';
                    }
                }
                $return .= ");\n";
            }
        }

        $return .= "\n\n\n";
    }

    $handle = fopen('your_db_'.time().'.sql','w+');
    fwrite($handle,$return);
    fclose($handle);
    echo "Database Export Successfully!";
}

backupDatabaseAllTables('localhost','root','','crud');
?>

Categories:

MYSQLPHP

Other Articles

laravel passport api
Previous

How to integrate passport API in Laravel

Top 9 HTTP Request Methods
Next

Top 9 HTTP Request Methods

4 Comments

  1. Image Mehtab Ahmed says:
    August 6, 2021 at 7:52 am

    it works great, it is very helpful for me. Thanks.

    Reply
    1. Image Dharmesh Solanki says:
      August 16, 2021 at 1:10 pm

      Do Share this to Others

      Reply
  2. Image Perawat says:
    June 1, 2022 at 5:35 am

    Thanks Very much IT WORKk

    Reply
  3. Image Zied says:
    July 20, 2022 at 1:24 pm

    Hi thank you for you effort, i want this script but i want to make it not with INSERT query i want it with UPDATE and without the CREATE table
    all good my problem is how to change the INSERT Query to UPDATE i need the column name
    to say SET column_name = column_value , where to find those values in this script , any help ? and thank .

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Related Posts

MySQL Error 121 Duplicate key on write - Practical Repair Strategy

MySQL Error 121 Duplicate key on write – Practical Repair Strategy

February 13, 2026
PHP cURL error 60: SSL certificate problem - Windows & Linux Fix

PHP cURL error 60: SSL certificate problem – Windows & Linux Fix

February 11, 2026
MySQL 8 Error Unknown collation: utf8mb4_0900_ai_ci - Safe Migration Guide

MySQL 8 Error Unknown collation: utf8mb4_0900_ai_ci – Safe Migration Guide

February 4, 2026
PHP 8.3 Fatal Error: Cannot use string offset as an array - Real World Case

PHP 8.3 Fatal Error: Cannot use string offset as an array – Real World Case

January 30, 2026
Devnote

Devnote provides a collection of tutorials about PHP, Laravel, WordPress, Django, MySQL, Bootstrap, Jquery, Ajax, APIs, CRUD operations, etc.

Devnote © , All Rights Reserved.

Page Links

  • Home
  • About us
  • Blog
  • Contact US
  • Site map

Category

  • PHP
  • Laravel
  • wordpress
  • HTML
  • jQuery

Follow Us

Facebook Instagram Twitter Youtube Linkedin Pinterest
Morbi, Gujarat, India - 363641
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Cookie settingsACCEPT
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT
Advertisement