How do i download a .sql file in php?

How do i download a .sql file in php?

Importing SQL script via programming will be useful when we need to create database structure dynamically. For example, if we provide APP or plugin to download and install from online, the dynamic SQL import will be used to setup the application database at the time of installing the APP or plugin.

To import SQL file in the database, you have to login to your hosting server or phpMyAdmin. Also, you can import the database from PHP script without login to your hosting server or phpMyAdmin.

Import database from PHP script is extremely helpful when you need to enable the client to import the database from your web application. A backup of the database should be taken for importing tables in MySQL database.

In this tutorial, we are going to import a SQL file into a database using PHP programming. In this example, we have a file containing SQL for creating contact table and inserting data. The PHP code parses the file line by line and extracts the query statements to execute using PHP MySQL function.

Using the importDatabaseTables() work in PHP, you can import tables in the database from a .sql file. The following given parameters are required to import or rstore MySQL database using PHP.

  • $dbHost – Required. Specifies the host name of the database.
  • $dbUname – Required. Specifies the database username.
  • $dbPass – Required. Specifies the database password.
  • $dbName – Required. Specifies the database name in which you wants to import.
  • $filePath – Required. Specifies the path of the SQL file from where the tables will be imported.

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

<?php

/**

* @function    importDatabaseTables

* @author      Tutorialswebsite

* @link        http://www.tutorialswebsite.com

* @usage       Import database tables from a SQL file

*/

$dbHost     ='localhost';

$dbUname='root';

$dbPass='';

$dbName     ='DB Name';

$filePath   = 'files/backupfile.sql';

if(file_exists($filePath)){

importDatabaseTables($dbHost,$dbUname,$dbPass,$dbName,$filePath);

}

functionimportDatabaseTables($dbHost,$dbUname,$dbPass,$dbName,$filePath){

    // Connect & select the database

    $db =newmysqli($dbHost,$dbUname,$dbPass,$dbName);

    // Temporary variable, used to store current query

    $templine ='';

    // Read in entire file

    $lines=file($filePath);

    $error='';

    // Loop through each line

    foreach($linesas$line){

        // Skip it if it's a comment

        if(substr($line, 0,2)=='--'||$line==''){

            continue;

        }

        // Add this line to the current segment

        $templine.=$line;

        // If it has a semicolon at the end, it's the end of the query

        if (substr(trim($line),-1,1)==';'){

            // Perform the query

            if(!$db->query($templine)){

                $error.='Error importing query "<b>'.$templine.'</b>": '.$db->error. '<br /><br />';

            }

            // Reset temp variable to empty

            $templine='';

        }

    }

    return!empty($error)?$error:true;

}

?>

Are you want to get implementation help, or modify or extend the functionality of this script? Submit paid service request

How do i download a .sql file in php?

Pradeep Maurya is the Professional Web Developer & Designer and the Founder of  “Tutorials website”. He lives in Delhi and loves to be a self-dependent person. As an owner, he is trying his best to improve this platform day by day. His passion, dedication and quick decision making ability to stand apart from others. He’s an avid blogger and writes on the publications like Dzone, e27.co

Post navigation

How do I download a .SQL file?

Instructions on Exporting a Database.
Select the database you would like to work with..
Along the top of the screen click the "Export" tab..
Select the options you would like for the export. If you aren't sure, simply leave things as they are and click the "Save as file" checkbox and click Go..
This will download a ..

How do I run a .SQL file in PHP?

You should run your . sql script in PHP by invoking the mysql tool, for instance with shell_exec() . I got this test working: $command = "mysql --user={$vals['db_user']} --password='{$vals['db_pass']}' " .

How can I download uploaded file from database in PHP?

'); } else { // Connect to the database $link=mysql_connect("localhost","*****","*****") or die(mysql_error()); mysql_select_db("RadReq", $link) or die(mysql_error()); mysql_set_charset('utf-8'); // Fetch the file information $query = "SELECT * FROM formdata WHERE form_id = $id"; $result=mysql_query($query); if($result ...

How can I download MySQL database in PHP?

php $DB_HOST = "localhost"; $DB_USER = "root"; $DB_PASS = "admin"; $DB_NAME = "dbname"; $con = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME); $tables = array(); $result = mysqli_query($con,"SHOW TABLES"); while ($row = mysqli_fetch_row($result)) { $tables[] = $row[0]; } $return = ''; foreach ($tables as $table) { ...