Posts

Mysqlexport – Exporting the Database with PHP

Whether you are a programmer or a website owner, it’s up to everyone; you want to back up your databases but you use shared hosting and it becomes impossible to export and back up your databases because of problems from the hosting company. Even if you do not have such a problem, you can manage your own server like me, and it’s hard for you to back up many databases manually. You would like to have a system (PHP script) that automatically backs up all your databases. Anyway, the programmer is lazy, the programmers are people who automate or facilitate everything to stay lazy.

I also created a PHP class to automatically back up the databases on my server. I will get rid of this problem by creating a cronjob. You can either use it for automatic backup like me or you can create a custom page to backup with a password you will enter via the web.

You can find my “mysqlexport” class on github. You can back up your databases as you like with the following sample usage. You should only do this with a user who has access to all databases. Otherwise, it will only back up the database if it has permission.

Usage Steps;

  1. First, we include our class.
  2. We create a new “mysqlexport” class instance. In the first parameter, we write the path to be backed up. We then write the database connection address (usual localhost), the username, and the password as parameters.
  3. After that, we have two choices; we define the databases we want to back up in an array with setDatabases(array $databases), or we get the names of the databases with the getDatabases() function.
  4. Finally, we end the backup with the export() function.
<?php
include 'Export.php';
$export = new MysqlExport\Export('backup', 'localhost', 'root', 'dev1');
$export->getDatabases();
$export->export();

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

 

GELECEĞİN TRENDİ BÜYÜK VERİ (BİG DATA)

Günümüzde bilişim teknolojileri o kadar hızlı gelişiyor ki takip etmeyi bırakalım haberdar olmamız bile güçleşmiş durumdadır. Çünkü biz o yeni teknolojiden haberdar olana kadar bazen onun bir üst versiyonu ya da yerini alabilecek teknoloji çıkmış olabiliyor.

Şu anda da birçok IT uzmanı tarafından konu edilen Büyük Veri kavramı çıktı ortaya. Henüz yaygınlaşmasa da şimdiden birçok kişinin hedeflerinde yer almış durumda ve ilerleyen zamanlarda çok etkin bir şekilde kullanılacağı kesin. Peki, nedir bu Büyük Veri?

Read more