<?php
 
// example for using 2 diffrent type of database
 
 
ini_set('display_errors', 1);
 
error_reporting(E_ALL);
 
define('FAR_ANTIHACK', true); // required - to access the class should set this constant
 
 
// mysqli settings
 
$mysqli_info = array(            
 
    'server'        => '127.0.0.1',            // required - for "localhost" port 3306 is used, for other port use ip address
 
    'port'          => 3306,                // optional - if use other port, dont put "localhost" on server name (see coments on http://ro1.php.net/manual/en/mysqli.construct.php )
 
    'username'      => 'db_user',           // required - sql user for mysql (for sqlite is empty string '')
 
    'password'      => 'db_pass',            // required - sql password for mysql (for sqlite is empty string '')
 
    'db_name'       => 'test',                // required - database name (for sqlite is optional)
 
    'db_encoding'   => 'utf8',              // optional - for mysql is used utf8 default
 
    'db_persist'    => false,               // optional - is not implemented on mysqli (read http://www.php.net/manual/en/mysqli.persistconns.php if you want to use persistent connection)
 
);
 
 
// sqlite3 settings
 
$sqlite3_info = array(            
 
    'server'        => 'path-to-sql-file/sqlite-name.db',  // required - for "localhost" port 3306 is used, for other port use ip address
 
    'port'          => 0666,                // is not used on sqlite
 
    'username'      => '',                  // is not used on sqlite
 
    'password'      => '',                    // is not used on sqlite
 
    'db_name'       => 'test2',                // required - database name
 
    'db_encoding'   => 'utf8',              // optional
 
    'db_persist'    => false,               // is not used on sqlite
 
);
 
 
// include sql class
 
if ( file_exists('mysqli.inc.php') )
 
    include_once 'mysqli.inc.php';
 
else
 
    die('no file mysqli.inc.php - check if file exists and is put on correct path');
 
 
if ( file_exists('sqlite3.inc.php') )
 
    include_once 'sqlite3.inc.php';
 
else
 
    die('no file sqlite3.inc.php - check if file exists and is put on correct path');
 
 
// start first sql connection
 
$db1 = new MYSQLI_DB($mysqli_info['server'], $mysqli_info['port'], $mysqli_info['username'], $mysqli_info['password'], $mysqli_info['db_name'], $mysqli_info['db_encoding']);
 
if( ! is_object($db1->conn) || $db1->sql_tracer[0]['error_nr'] == 1045 ) {
 
    echo '<br>Error initializing the database connection.<br>';
 
    echo '<pre>'.var_export($db1->sql_tracer,1).'</pre>';
 
    exit;
 
} else {
 
    echo '<br>Specified database connection was made successfully.';
 
    //echo '<pre>'.var_export($db1->sql_tracer,1).'</pre>';
 
    echo '<hr>';
 
}
 
 
// start second sql connection
 
$db2 = new SQLITE3_DB($sqlite3_info['server'], $sqlite3_info['port'], $sqlite3_info['username'], $sqlite3_info['password'], $sqlite3_info['db_name'], $sqlite3_info['db_encoding']);
 
if ( $db2->open() === false ) {
 
    echo '<br>Error initializing the database connection.<br>';
 
    echo '<pre>'.var_export($db2->sql_tracer,1).'</pre>';
 
    echo $db2->sql_error();
 
    exit;
 
} else {
 
    echo '<br>Specified database connection was made successfully.';
 
    //echo '<pre>'.var_export($db2->sql_tracer,1).'</pre>';
 
    echo '<hr>';
 
}
 
 
// start reading data from first db
 
$info = array();
 
$query = "SELECT * FROM test";
 
$rezult = $db1->query($query);
 
if ( ! $rezult ) {
 
    echo '<br>An error occurred reading data from the database.<br>';
 
    echo $db1->sql_error();    
 
    exit;
 
} else {
 
    while($row=$db1->fetch_array()) {
 
        $info[] = $row;
 
    }
 
    // erasing the memory
 
    $db1->free_result();
 
}
 
// end reading data
 
 
// save data to second db
 
if ( count($info) ) {
 
    foreach($info as $row) {
 
        $query = "INSERT INTO table_name (col1, col2) VALUES ('{$row['col1']}', '{$row['col2']}') ";
 
        $rezult = $db2->query($query);
 
        if ( ! $rezult ) {
 
            echo '<br>An error occurred in data entry in the table.<br>';
 
            echo $db2->sql_error();
 
            exit;
 
        } else {
 
            $id = $db2->insert_id();
 
            echo sprintf('<br>Data are stored in the database. Id returned is %s', $id);
 
        }
 
    }
 
}
 
 
// show log if is needed (for debug)
 
// echo '<pre>'.var_export($db1->sql_tracer,1).'</pre>';
 
// show all query if is needed (for debug)
 
// echo '<pre>'.var_export($db1->sql_query_log,1).'</pre>';
 
// show total query
 
echo '<br>Total query sql1: '.count($db1->sql_query_log);
 
echo '<br>Total query sql2: '.count($db2->sql_query_log);
 
// this is optional (destructor of the class close sql connection automaticaly)
 
//$db1->close();
 
 
//unset($db);
 
// done
 
?>
 
 |