Import Data From Mysql to Mongodb With Ezsql Class

Import data from Mysql to MongoDb with EzSql class
by Janeth Kent Date: 11-04-2013


This simple php class to demonstrate how to import data from a MySql database to a Mongodb with out complications. 




// It uses the ezsql database class from Justin Vincent: 
// http://justinvincent.com/ezsql

include_once "ezsql/shared/ez_sql_core.php";
include_once 
"ezsql/mysql/ez_sql_mysql.php";
          
class 
mongodb_importer
{
    public 
$user;    
    public 
$pass;    
    public 
$databse;    
    public 
$server;    
    public function 
import($table_name,$mongo_db)
    {
        
$db = new ezSQL_mysql($this->user,$this->pass,$this->database,$this->server);
            
        
$m = new Mongo();
        
$mongo_db $m->selectDB($mongo_db);
        
$collection = new MongoCollection($mongo_db$table_name);
        
$res $db->get_results("select * from "$table_name);
        
$i 0;
        foreach(
$res as $r)
        {
            
$i++;
            
$collection->insert($r);    
        }
        return 
$i;
    }
}

?>


And here it is an example about how to use this!

    // create a new object
    
$importer = new mongodb_importer();
    
    
// set your Mysql information
    
$importer->database "my_databse";
    
$importer->pass "my_secret_password";
    
$importer->user "my_user_name";
    
$importer->server "localhost";
    
    
// import the data
    
$count $importer->import("user","new_db");
    
    
// print the result
    
echo "Imported " $count " records.";
?>
 
by Janeth Kent Date: 11-04-2013 hits : 2610  
 
Janeth Kent

Janeth Kent

Licenciada en Bellas Artes y programadora por pasión. Cuando tengo un rato retoco fotos, edito vídeos y diseño cosas. El resto del tiempo escribo en MA-NO WEB DESIGN END DEVELOPMENT.

 
 
 

We use our own and third-party cookies to improve our services, compile statistical information and analyze your browsing habits. This allows us to personalize the content we offer and to show you advertisements related to your preferences. By clicking "Accept all" you agree to the storage of cookies on your device to improve website navigation, analyse traffic and assist our marketing activities. You can also select "System Cookies Only" to accept only the cookies required for the website to function, or you can select the cookies you wish to activate by clicking on "settings".

Accept All Only sistem cookies Configuration