BPK PENABUR Jakarta

Iman, Ilmu dan Pelayanan

Cara Backup Database

Chrysanthemum

Berikut cara backup database:

<pre lang="php">
<?php

function backupSQL($db)
{
$tables=mysql_list_tables($db);

while ($td = mysql_fetch_array($tables))
{
$table = $td[0];
$r = mysql_query(“SHOW CREATE TABLE `$table`”);
if ($r)
{
$insert_sql = “”;
$d = mysql_fetch_array($r);
$d[1] .= “;”;
$SQL[] = str_replace(“\n”, “”, $d[1]);
$table_query = mysql_query(“SELECT * FROM `$table`”);
$num_fields = mysql_num_fields($table_query);

if(mysql_num_rows($table_query) > 0)
$insert_sql .= “INSERT INTO $table VALUES “;

$i = 0;
while ($fetch_row = mysql_fetch_array($table_query))
{
$insert_sql .= “(“;
for ($m=0;$m<$num_fields;$m++)
{
$value = mysql_real_escape_string($fetch_row[$m]);
$type  = mysql_field_type($table_query, $m);

if(strcmp($type, “int”) == 0 && is_numeric($value))
$insert_sql .= $value.”, “;
elseif(is_null($fetch_row[$m]))
$insert_sql .= “NULL, “;
else
$insert_sql .= “‘”.$value.”‘, “;
}
$insert_sql = substr($insert_sql,0,-2);

if($i == mysql_num_rows($table_query) – 1)
$insert_sql .= “);\n”;
else
$insert_sql .= “),\n”;

$i++;
}

if ($insert_sql!= “”)
{
$SQL[] = $insert_sql;
}
}
}

$sql = implode(“\r”, $SQL);
$date = date(“Y-m-d”);
$name = $db.”.sql”;
$fileName = fopen ($name, ‘w’);
fputs ($fileName, $sql);
fclose ($fileName);
}

?>
</pre>

All about Information Technology in BPK PENABUR Jakarta

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Information

This entry was posted on June 25, 2013 by and tagged .
%d bloggers like this: