Home Backend Development PHP Tutorial How to Successfully Import .sql Files into MySQL Using PHP?

How to Successfully Import .sql Files into MySQL Using PHP?

Oct 29, 2024 am 05:49 AM

How to Successfully Import .sql Files into MySQL Using PHP?

Importing .sql Files into MySQL Using PHP

When attempting to import a .sql file through PHP, an error may arise, indicating that the import file is not in the same folder as the script or that the values are incorrect.

Determining the Issue

The provided code executes a command using the exec() function to import the .sql file. However, the error message suggests that the import file cannot be found or that the values for the database connection are incorrect.

Alternative Approach

Rather than using the exec() function, a more reliable method is to use the MySQLi extension, which provides explicit support for MySQL database interactions in PHP.

Revised Code

1

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

<code class="php">&lt;?php

// Name of the SQL file to import

$filename = 'dbbackupmember.sql';

 

// MySQL connection credentials

$hostname = 'localhost';

$username = 'root';

$password = '';

$database = 'test';

 

// Create a new MySQLi connection

$mysqli = new mysqli($hostname, $username, $password, $database);

if ($mysqli-&gt;connect_errno) {

    echo "Failed to connect to MySQL: (" . $mysqli-&gt;connect_errno . ") " . $mysqli-&gt;connect_error;

    exit;

}

 

// Get the contents of the SQL file

$sql = file_get_contents($filename);

 

// Execute the SQL statements

$result = $mysqli-&gt;multi_query($sql);

 

// Check the execution status

if ($result) {

    echo "SQL file imported successfully.";

} else {

    echo "Error importing SQL file: " . $mysqli-&gt;error;

}

 

// Close the connection

$mysqli-&gt;close();

?&gt;</code>

Copy after login

In this code:

  • A new MySQLi connection is created using the provided credentials.
  • The contents of the .sql file are read into the $sql variable.
  • The multi_query() function is used to execute all the SQL statements in the file one by one.
  • The status of the execution is checked, and an appropriate message is displayed.

The above is the detailed content of How to Successfully Import .sql Files into MySQL Using PHP?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

11 Best PHP URL Shortener Scripts (Free and Premium)

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Working with Flash Session Data in Laravel

Introduction to the Instagram API Introduction to the Instagram API Mar 02, 2025 am 09:32 AM

Introduction to the Instagram API

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Simplified HTTP Response Mocking in Laravel Tests

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

Build a React App With a Laravel Back End: Part 2, React

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

cURL in PHP: How to Use the PHP cURL Extension in REST APIs

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

12 Best PHP Chat Scripts on CodeCanyon

Notifications in Laravel Notifications in Laravel Mar 04, 2025 am 09:22 AM

Notifications in Laravel

See all articles