-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysqlToFixture.php
49 lines (37 loc) · 1.15 KB
/
mysqlToFixture.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
<?php
//================================================
// Settings:
$dbName = "demo";
$dbUser = "root";
$dbPass = "";
$dbServer = "127.0.0.1";
$tableName = "user";
//================================================
$fixtureOutput = "<?php\n";
$fixtureOutput .= "\n";
$fixtureOutput .= "/**\n";
$fixtureOutput .= " * Autogenerated by MysqlToFixture\n";
$fixtureOutput .= " * Michael Kling - Ascendro Technologies S.R.L\n";
$fixtureOutput .= " * Date: ".date("Y-m-d H:i:s")."\n";
$fixtureOutput .= " */\n";
$fixtureOutput .= "\n";
$fixtureOutput .= "return array(\n";
$db = new mysqli($dbServer, $dbUser, $dbPass, $dbName);
if($db->connect_errno > 0){
die('Unable to connect to database [' . $db->connect_error . ']');
}
$sql = "SELECT * FROM `$tableName`";
if(!$result = $db->query($sql)){
die('There was an error running the query [' . $db->error . ']');
}
$i = 0;
while($row = $result->fetch_assoc()){
$fixtureOutput .= "\t'{$tableName}_{$i}' => array(\n";
$i++;
foreach ($row as $columnName => $column) {
$fixtureOutput .= "\t\t'{$columnName}' => '{$column}',\n";
}
$fixtureOutput .= "\t),\n";
}
$fixtureOutput .= ");\n";
echo $fixtureOutput;