PHP ile SQL Dosyası Nasıl İçe Aktarılır
Bazen PhpMyAdmin veya herhangi bir veritabanı programını kullanamayabilir ve SQL dosyasını veritabanınıza aktarmak için başka seçenekler kullanmak zorunda kalabilirsiniz. Burada, hem eski mysql_query hem de yeni mysqli_query ile PHP‘de nasıl içe aktarabileceğinizi anlatacağım. Kodlara Github projemden de ulaşabilirsiniz.
Her iki yöntemde de yapmanız gerekenler şunlardır: SQL dosyasını ve PHP dosyasını aynı yere yükleyin ve PHP dosyasını site adresi üzerinden çalıştırın. Konsol komutlarını sunucu üzerinden de çalıştırabilirsiniz.
$filename değişkenini SQL adıyla güncelleyin. $mysql_host değişkenini veritabanı sunucusuyla güncelleyin. Veritabanı sunucusu farklı değilse, olduğu gibi bırakabilirsiniz. $mysql_username ve $mysql_password, veritabanınızın kullanıcı adı ve parolasıdır. $mysql_database veritabanı adıdır, veritabanı adınızla güncelleyin.
mysql_query fonksiyonu ile
<?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 ile
<?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);