Posts

How To Import SQL File With PHP

Sometimes there are times when you can not use PhpMyAdmin or any database program, and you have to use other options to import the Sql file into your database. Here, I’ll tell you how you can import with both the old mysql_query and the new mysqli_query in Php. You can also access the codes from the my Github project.

You need to do followings on both methods; upload Sql file and php file at the same place and run the php file through the site address. You can also run console commands through the server.

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

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);

importSqlWithPhp – Php ile Sql Dosyası Yükleme (Import Etme)

Bazen Phpmyadmin ya da herhangi bir veritabanı programı kullanamadığınız zamanlar olur ve Sql dosyasını veritabanına yüklemek için başka seçenekler kullanmak zorunda kalırsınız. Burada ise Php‘de hem eski mysql_query hem de yeni mysqli_query kullanarak nasıl yükleyebileceğinize dair bilgilendirme yapacağım. Dosyalara aynı zamanda Github projemden de ulaşabilirsiniz.

Her iki yöntemde de yapmanız gereken şunlardır. Sql dosyası ile oluşturacağınız php dosyasını aynı yere yükleyip site adresi üzerinden php dosyasını çalıştırmak. Sunucu üzerinden konsol komutları ile de çalıştırabilirsiniz.

$filename değişkeni dosya ismi ile güncelleyin. $mysql_host ismini ise veritabanı sunucusu ile güncelleyin. Veritabanı sunucusu farklı değilse olduğu gibi bırakabilirsiniz. $mysql_username ve $mysql_password veritabanı için oluşturduğunuz kullanıcı adı ve şifredir. $mysql_database ise veritabanı ismidir. Gerekli şekilde güncellersiniz.

Mysql_query Fonksiyonu İle

<?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";
?>

Mysqli_query Class’ı İle

<?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);