Hongmu Notes
Home Language Notes PHP database connection and SQL file import.
Language Notes PHP

PHP database connection and SQL file import.

PHP database connection and SQL file import.

To connect to a database and import an SQL file in PHP, you can use the MySQLi extension. Here is a simple example of code that demonstrates how to connect to a database and execute an SQL file:

// 配置数据库连接信息
$host = "localhost"; // 数据库主机名
$user = "username"; // 数据库用户名
$password = "password"; // 数据库密码
$dbname = "database_name"; // 数据库名称

// 创建数据库连接
$conn = mysqli_connect($host, $user, $password, $dbname);

// 检查连接是否成功
if (!$conn) {
    die("连接失败: " . mysqli_connect_error());
}

// 读取 SQL 文件内容
$sql = file_get_contents("path/to/file.sql");

// 执行 SQL 文件内容
if (mysqli_multi_query($conn, $sql)) {
    echo "SQL 文件导入成功!";
} else {
    echo "导入失败: " . mysqli_error($conn);
}

// 关闭数据库连接
mysqli_close($conn);

In the above code, we first configure the database connection details, then use the mysqli_connect() function to establish a database connection. Next, we use the file_get_contents() function to read the contents of the SQL file and then use the mysqli_multi_query() function to execute multiple SQL statements contained within that file. Finally, we close the database connection.

Please note that the above code is only applicable when the SQL file contains multiple SQL statements. If your SQL file contains only a single SQL statement, you can use the mysqli_query() function to execute that statement.

微信赞赏

WeChat

支付宝赞赏

Alipay

✍️ Author: Hong Mu

webmaster · Thanks for reading, stay tuned for more exciting content

Author homepage View home page →

Related articles

PHP ob function record

PHP ob function record Language Notes PHP

Usage of the following three functions ob_get_contents(); ob_end_clean(); ob_start(); You can use these functions to buffer local files and execute local script code. Use ob_start() to save the output code into the buffer, and the page will not be displayed; then use ob_get_contents to get the data in the buffer. o…
👁 143
PHP preg

PHP preg Language Notes PHP

The preg_match_all function is used to perform a global regular expression match. preg_match_all() Syntax int preg_match_all ( string $pattern , string $subject [, array &$matches [, int $flags = PREG…
👁 169
Detailed explanation of PHP ternary operator and if

Detailed explanation of PHP ternary operator and if Language Notes PHP

Ternary operator condition ? Result 1 : Result 2 Explanation: The position in front of the question mark is the condition for judgment. If the condition is met, the result is 1, and if it is not met, the result is 2. This article compares and explains the ternary operator and if...else... in detail. I hope it will be helpful to everyone. Today when I was revising my paper online, I encountered a statement that I couldn’t understand: $if_summary = $row['IF_SUMMARY']=…
👁 191
PHP cast type

PHP cast type Language Notes PHP PHP collection PHP and mysql

Get the data type 1. If you want to check the value and type of an expression, use var_dump(). 2. If you just want to get an easy-to-read type expression for debugging, use gettype(). 3. To check a certain type, do not use gettype(), but use the is_type() function. Converting Strings to Numbers When a string is evaluated as a number, the result is determined according to the following rules...
👁 232

Recommended reading

(Adaptive Mobile Version) LED Lighting – pbootCMS Website Template; Download LED Green Lighting Website Source Code – 0547

(Adaptive Mobile Version) LED Lighting – pbootCMS Website Template; Download LED Green Lighting Website Source Code – 0547 Practical Collection pbootcms Template

An LED lighting and green lighting PbootCMS website template supporting mobile access. Featuring a modern, energy-efficient design, it is ideal for LED lighting manufacturers and lighting engineering companies to showcase their products. This template helps lighting enterprises present their brands online and attract engineering and international trade clients. Template Display | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5.cn...
👁 53
Household Service & Cleaning Website Template 0164

Household Service & Cleaning Website Template 0164 Practical Collection Yiyou template

An EyouCMS website template designed for the domestic services and cleaning industry. Its fresh and professional design allows you to showcase cleaning services, domestic service offerings, service personnel, and customer reviews. This template helps domestic service companies build a strong online brand presence and attract residential clients. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS | EyouCMS (...
👁 31
Clear Linux command-line history

Clear Linux command-line history Summary of pitfalls

In Linux systems, historical command-line history is typically stored in the `bash_history` file located in the user's home directory. You can clear this history using the following methods: Manual deletion – Open a terminal and use the `cd` command to navigate to your home directory (for example, if your username is "username", your home directory is `/home/username`)...
👁 476
Responsive jewelry and diamond jewelry website template – 0978

Responsive jewelry and diamond jewelry website template – 0978 Practical Collection Yiyou template

An eyouCMS responsive website template designed for the jewelry, diamond, and accessory industry. Its premium, luxurious design is ideal for showcasing jewelry products, diamond jewelry pieces, brand stories, and customisation services. This template helps jewelry brands present their products online and attract high-net-worth customers. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for YououCMS | YououCMS...
👁 30
(PC + WAP) pbootCMS – Eco-friendly, versatile corporate website template; Download source code for general construction industry websites – 0548

(PC + WAP) pbootCMS – Eco-friendly, versatile corporate website template; Download source code for general construction industry websites – 0548 Practical Collection pbootcms Template

A eco-friendly PbootCMS universal corporate website template ideal for the construction, engineering, and environmental protection industries, compatible with both PC and mobile devices. Its minimalist yet sophisticated design aligns perfectly with the brand identity of companies operating in the construction and environmental sectors. The template includes a comprehensive corporate showcase module, enabling businesses to quickly launch their branded websites and effectively build customer trust. Template Display | Installation Instructions | Website Backend: /admin.php | Username: admin | Password:...
👁 43
Responsive Smart Home Appliance Website Template 0165

Responsive Smart Home Appliance Website Template 0165 Practical Collection Yiyou template

This EyouCMS responsive template is ideal for the smart home and household appliance industry, featuring a modern, tech-driven design style that effectively showcases smart home products, household appliances, system solutions, and brand strength. It helps smart home enterprises present their products online and attract consumers who value a high-quality lifestyle. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Common Questions Regarding EyouCMS Installation...
👁 44