<?php
 
// example page for using 2 databases
 
 
ini_set('display_errors', 1);
 
error_reporting(E_ALL);
 
define('FAR_ANTIHACK', true); // required - to access the class should set this constant
 
 
// sql settings
 
$sql1_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_user1',           // required - sql user for mysql (for sqlite is empty string '')
 
    'password'      => 'db_pass1',            // required - sql password for mysql (for sqlite is empty string '')
 
    'db_name'       => 'test1',                // 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)
 
);
 
 
// sql settings
 
$sql2_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_user2',           // required - sql user for mysql (for sqlite is empty string '')
 
    'password'      => 'db_pass2',            // required - sql password for mysql (for sqlite is empty string '')
 
    'db_name'       => 'test2',                // 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)
 
);
 
 
// 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');
 
    
 
// start first sql connection
 
$db1 = new MYSQLI_DB($sql1_info['server'], $sql1_info['port'], $sql1_info['username'], $sql1_info['password'], $sql1_info['db_name'], $sql1_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($db->sql_tracer,1).'</pre>';
 
    echo '<hr>';
 
}
 
 
// start second sql connection
 
$db2 = new MYSQLI_DB($sql2_info['server'], $sql2_info['port'], $sql2_info['username'], $sql2_info['password'], $sql2_info['db_name'], $sql2_info['db_encoding']);
 
if( ! is_object($db2->conn) || $db2->sql_tracer[0]['error_nr'] == 1045 ) {
 
    echo '<br>Error initializing the database connection.<br>';
 
    echo '<pre>'.var_export($db2->sql_tracer,1).'</pre>';
 
    exit;
 
} else {
 
    echo '<br>Specified database connection was made successfully.';
 
    //echo '<pre>'.var_export($db->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
 
?>
 
 |