


Importing a Large MySQL File in PHP: A Comprehensive Split-Query Solution
Importing a large MySQL file into a shared hosting provider website can be a challenge, especially when using PHP and external access to MySQL is restricted. This article provides a comprehensive solution that addresses this issue by reliably splitting the file into individual queries for import.
Challenges to Consider
When parsing and querying a large MySQL file in PHP, several challenges arise:
- Detecting field delimiters within data
- Handling line breaks in memo fields
- Avoiding memory issues due to large file size
A Reliable Split-Query Solution
To overcome these challenges, this article introduces a memory-friendly function called SplitSQL(). This function reads the file line-by-line and accumulates individual queries until a query delimiter (such as a semicolon) is encountered. Here is the code:
function SplitSQL($file, $delimiter = ';') { set_time_limit(0); if (is_file($file) && is_resource($file = fopen($file, 'r'))) { $query = array(); while (!feof($file)) { $query[] = fgets($file); if (preg_match('~' . preg_quote($delimiter, '~') . '\s*$~iS', end($query))) { $query = trim(implode('', $query)); if (mysql_query($query) === false) { echo 'ERROR: ' . $query . "\n"; } else { echo 'SUCCESS: ' . $query . "\n"; flush(); } $query = array(); } } fclose($file); return true; } return false; }
Memory-Friendly and Tested
This function allocates memory only as needed, avoiding memory issues. It has been tested on large phpMyAdmin SQL dumps, demonstrating its ability to handle real-world data effectively.
Test Data and Output
Sample data and its respective output using the SplitSQL() function:
Test Data:
CREATE TABLE IF NOT EXISTS "test" ( "id" INTEGER PRIMARY KEY AUTOINCREMENT, "name" TEXT, "description" TEXT ); BEGIN; INSERT INTO "test" ("name", "description") VALUES (";;;", "something for you mind; body; soul"); COMMIT; UPDATE "test" SET "name" = "; " WHERE "id" = 1;
Output:
SUCCESS: CREATE TABLE IF NOT EXISTS "test" ( "id" INTEGER PRIMARY KEY AUTOINCREMENT, "name" TEXT, "description" TEXT ); SUCCESS: BEGIN; SUCCESS: INSERT INTO "test" ("name", "description") VALUES (";;;", "something for you mind; body; soul"); SUCCESS: COMMIT; SUCCESS: UPDATE "test" SET "name" = "; " WHERE "id" = 1;
Conclusion
The SplitSQL() function provides a robust and reliable solution for splitting large MySQL files into individual queries, allowing efficient and automated database import in shared hosting environments. By addressing memory constraints and accounting for various data types, this function simplifies the complex process of importing large MySQL files in PHP.
The above is the detailed content of How to Import Large MySQL Files in PHP: A Memory-Efficient Solution with Split-Query Approach?. For more information, please follow other related articles on the PHP Chinese website!

The article discusses using MySQL's ALTER TABLE statement to modify tables, including adding/dropping columns, renaming tables/columns, and changing column data types.

Article discusses configuring SSL/TLS encryption for MySQL, including certificate generation and verification. Main issue is using self-signed certificates' security implications.[Character count: 159]

Article discusses strategies for handling large datasets in MySQL, including partitioning, sharding, indexing, and query optimization.

Article discusses popular MySQL GUI tools like MySQL Workbench and phpMyAdmin, comparing their features and suitability for beginners and advanced users.[159 characters]

The article discusses dropping tables in MySQL using the DROP TABLE statement, emphasizing precautions and risks. It highlights that the action is irreversible without backups, detailing recovery methods and potential production environment hazards.

Article discusses using foreign keys to represent relationships in databases, focusing on best practices, data integrity, and common pitfalls to avoid.

The article discusses creating indexes on JSON columns in various databases like PostgreSQL, MySQL, and MongoDB to enhance query performance. It explains the syntax and benefits of indexing specific JSON paths, and lists supported database systems.

Article discusses securing MySQL against SQL injection and brute-force attacks using prepared statements, input validation, and strong password policies.(159 characters)


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.