How to Import SQL File with PHP

Sometimes PhpMyAdmin or may not use any database program and SQL You may have to use other options to import the file into your database. Here, both the old mysql_query both new mysqli_query with PHPI'll explain how you can import it. You can also find the code on my Github project.

In both methods, you need to do the following: SQL file and PHP upload the file to the same location and PHP Run the file from the site address. You can also run console commands from the server.

$filename variable SQL update with name. $mysql_host Update the variable with the database server. If the database server is not different, you can leave it as is. $mysql_username And $mysql_passwordis the username and password of your database. $mysql_database is the database name, update it with your database name.

with the 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>&#039; . $templine . &#039;\&#039;: &#039; . mysql_error() . &#039;<br /><br />&#039;); // Reset temp variable to empty $templine = &#039;&#039;; } } echo &quot;Tables imported successfully&quot;;

with mysqli_query

<?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 &quot;Failed to connect to MySQL: &quot; . $con-&gt;connect_errno; echo &quot;<br/>Error: &quot; . $con-&gt;connect_error; } // Temporary variable, used to store current query $templine = &#039;&#039;; // Read in entire file $lines = file($filename); // Loop through each line foreach ($lines as $line) { // Skip it if it&#039;s a comment if (substr($line, 0, 2) == &#039;--&#039; || $line == &#039;&#039;) continue; // Add this line to the current segment $templine .= $line; // If it has a semicolon at the end, it&#039;s the end of the query if (substr(trim($line), -1, 1) == &#039;;&#039;) { // Perform the query $con-&gt;query($templine) or print(&#039;Error performing query \&#039;<strong>&#039; . $templine . &#039;\&#039;: &#039; . $con-&gt;error() . &#039;<br /><br />&#039;); // Reset temp variable to empty $templine = &#039;&#039;; } } echo &quot;Tables imported successfully&quot;; $con-&gt;close($con);
0 0 votes
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments