PHP import and export
God how I hate Webmin. No wonder everyone is using phpMyAdmin, everyone except my admin of course.
Simple export:
$db_name = "";
$db_password = "";
$db_link = mysql_connect("localhost", "root", $db_password);
mysql_select_db($db_name, $db_link);
mysql_query("SET NAMES UTF8");
$table = "";
function assoc_query_2D($sql, $id_name = false){
$result = mysql_query($sql);
$arr = array();
$row = array();
if($result){
if($id_name == false){
while($row = mysql_fetch_assoc($result))
$arr[] = $row;
}else{
while($row = mysql_fetch_assoc($result)){
$id = $row['id'];
$arr[$id] = $row;
}
}
}else
return 0;
return $arr;
}
function query_whole_table($table, $value = '*'){
$sql = "SELECT $value FROM $table";
return assoc_query_2D($sql);
}
$export_str = "";
$result = query_whole_table($table);
foreach($result as $record){
$export_str .= implode(";",$record) . "\n";
}
file_put_contents($table."_export.csv", $export_str);
Simple import:
$db_name = "";
$db_password = "";
$db_link = mysql_connect("localhost", "root", $db_password);
mysql_select_db($db_name, $db_link);
mysql_query("SET NAMES UTF8");
$import = file('data.sql');
foreach($import as $line){
if(!preg_match('/^--/',$line) && strlen($line) > 3){
echo $line."<br>";
$result = mysql_query($line);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
}
}