यदि आप php एप्लिकेशन के रन टाइम पर mysql टेबल आयात करना चाहते हैं तो यहां मैं आपको दिखाने जा रहा हूं कि आप PHP का उपयोग करके आसानी से mysql टेबल को कैसे पुनर्स्थापित कर सकते हैं। आम तौर पर आप PHPMyAdmin से MySQL डेटाबेस आयात करने के लिए उपयोग करते हैं, यह MySQL डेटाबेस आयात करने का सबसे आसान तरीका है, लेकिन यदि आप वर्डप्रेस, जूमला, ड्रूपल इत्यादि जैसे PHP एप्लिकेशन की स्थापना के दौरान डेटाबेस आयात करने के लिए समाधान ढूंढ रहे हैं तो नीचे सरल PHP विधि है PHPMyAdmin के बिना mysql डेटाबेस आयात करना।

PHP का उपयोग करके MySql तालिकाओं को आयात करना
mysql डेटाबेस तालिकाओं को आयात/पुनर्स्थापित करने के लिए निम्न php स्क्रिप्ट का उपयोग करें।
<?php
// Set database credentials
$hostname = 'localhost'; // MySql Host
$username = 'root'; // MySql Username
$password = 'root'; // MySql Password
$dbname = 'dbname'; // MySql Database Name
// File Path which need to import
$filePath = 'sql_files/mysql_db.sql';
// Connect & select the database
$con = new mysqli($hostname, $username, $password, $dbname);
// Temporary variable, used to store current query
$templine = '';
// Read in entire file
$lines = file($filePath);
$error = '';
// Loop through each line
foreach ($lines as $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(!$con->query($templine)){
$error .= 'Error performing query "<b>' . $templine . '</b>": ' . $db->error . '<br /><br />';
}
// Reset temp variable to empty
$templine = '';
}
}
$con->close();
echo !empty($error)?$error:"Import Success";
?> |