importSqlWithPhp – Loading (Importing) SQL Files with Php
Sometimes Phpmyadmin or there are times when you cannot use any database program and SQL You will have to use other options to load the file into the database. Here, PHPin both old mysql_query both new mysqli_query I will give you information on how to upload using . The files can also be downloaded From my Github project You can also reach.
In both methods, you need to do the following: SQL file you will create with PHP upload the file to the same location and download it via the site address PHP You can also run the file using console commands on the server.
Update the $filename variable with the file name. Update the $mysql_host name with the database server. If the database server is not different, you can leave it as is. $mysql_username and $mysql_password are the username and password you created for the database. $mysql_database is the database name. Update as necessary.
With Mysql_query Function
<?php // Name of the file $filename = 'sql.sql'; // MySQL host $mysql_host = 'localhost'; // MySQL username $mysql_username = 'username'; // MySQL password $mysql_password = 'password'; // Database name $mysql_database = 'database'; // Connect to MySQL server mysql_connect($mysql_host, $mysql_username, $mysql_password) or die('Error connecting to MySQL server: ' . mysql_error()); // Select database mysql_select_db($mysql_database) or die('Error selecting MySQL database: ' . mysql_error()); // Temporary variable, used to store current query $templine = ''; // Read in entire file $lines = file($filename); // 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 mysql_query($templine) or print('Error performing query \'<strong>' . $templine . '\': ' . mysql_error() . '<br /><br />'); // Reset temp variable to empty $templine = ''; } } echo "Tables imported successfully"; ?>
With Mysqli_query Class
<?php // Name of the file $filename = 'sql.sql'; // MySQL host $mysql_host = 'localhost'; // MySQL username $mysql_username = 'username'; // MySQL password $mysql_password = 'password'; // Database name $mysql_database = 'database'; // Connect to MySQL server $con = @new mysqli($mysql_host,$mysql_username,$mysql_password,$mysql_database); // Check connection if ($con->connect_errno) { echo "Failed to connect to MySQL: " . $con->connect_errno; echo "<br/>Error: " . $con->connect_error; } // Temporary variable, used to store current query $templine = ''; // Read in entire file $lines = file($filename); // 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 $con->query($templine) or print('Error performing query \'<strong>' . $templine . '\': ' . $con->error() . '<br /><br />'); // Reset temp variable to empty $templine = ''; } } echo "Tables imported successfully"; $con->close($con);
There was a problem with the hosting kaykaklı phpmyadmin import plugin. I successfully imported my database with this method, thank you very much.