-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.php
96 lines (91 loc) · 3.37 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
/*Created by
Abhishek Gahlot On March 2013
http://www.abhishek.it
Mysql To Mongodb Importer
*/
define('dbname', 'YOUR_DB_NAME'); //Enter database name of mysql. Mongo will also create the database of this name.
// db connection class using singleton pattern
class dbConn
{
private $dbhost = 'DATABASE_HOST'; //Enter mysql database host
private $dbusername = 'DATABASE_USERNAME'; //Enter mysql database username
private $dbpassword = 'DATABASE_PASSWORD'; //Enter mysql database password
// variable to hold connection object.
protected static $db;
// private construct
private function __construct()
{
try {
// assign PDO object to db variable
self::$db = new PDO('mysql:host=' . $this->dbhost . ';dbname=' . dbname, $this->dbusername, $this->dbpassword);
self::$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e) {
// Output error - would normally log this to error file rather than output to user.
echo "Connection Error: " . $e->getMessage();
}
}
// get connection function. Static method - accessible without instantiation
public static function getConnection()
{
// Guarantees single instance, if no connection object exists then create one.
if (!self::$db) {
// new connection object.
new dbConn();
}
// return connection.
return self::$db;
}
} //end class
class Mysql
{
public function showTables()
{
$dbname = dbname;
$db = dbConn::getConnection();
$query = $db->query("SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_TYPE='BASE TABLE' AND TABLE_SCHEMA='$dbname'");
// $query=$db->query('SELECT * FROM users');
while ($results = $query->fetch(PDO::FETCH_ASSOC)) {
foreach($results as $key => $val) {
// Tables name
$tableArray[] = $val;
}
}
// Send tables name data to columns function
$this->showColumns($tableArray);
}
private function showColumns($tableArray)
{
$db = dbConn::getConnection();
$dbname = dbname;
foreach($tableArray as $table => $tableName) { //foreach for every table name fetch table name
$query = $db->query("SELECT `COLUMN_NAME` FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `TABLE_SCHEMA`='$dbname' AND `TABLE_NAME`='$tableName'");
unset($columnArray); //unset columnarray so that previous values dont get mixed
while ($columnQuery = $query->fetch(PDO::FETCH_ASSOC)) {
foreach($columnQuery as $column => $columnname) { //foreach for every table name fetch column name
// columns name
$columnArray[] = $columnname;
}
} //Send columns name and Table name to Data function
$this->showData($tableName, $columnArray);
}
} //function show columns ends here
private function showData($tableName, $columnArray)
{
$dbname = dbname;
$MongoConnect = new MongoClient();
$db = $MongoConnect->$dbname;
$tableCreation = $db->createCollection($tableName);
$db = dbConn::getConnection();
$query = $db->query("SELECT * from `$tableName`");
while ($results = $query->fetch(PDO::FETCH_ASSOC)) {
$rowColumn_combined = array_combine($columnArray, $results); //combining column and data to insert
$tableCreation->insert($rowColumn_combined);
}
echo 'Successfully Imported Table ' . $tableName . "\n";
} //showdata function bracket
}
$obj = new mysql;
$obj->showTables();
?>