「PHP入門」PHPはMySQLデータベース仁対してバックアップするプログラム
サンプルコード:
<?php
mysql_connect('localhost’, 'startnews24_test’, 'StartNews24′);
mysql_select_db(startnews24_’test’);
if(copy_table('products’, 'products_bak’)) {
echo “成功\n";
}
else {
echo “失敗\n";
}
function copy_table($from, $to) {
if(table_exists($to)) {
$success = false;
}
else {
mysql_query(“CREATE TABLE $to LIKE $from");
mysql_query(“INSERT INTO $to SELECT * FROM $from");
$success = true;
}
return $success;
}
function table_exists($tablename, $database = false) {
if(!$database) {
$res = mysql_query(“SELECT DATABASE()");
$database = mysql_result($res, 0);
}
$res = mysql_query("
SELECT COUNT(*) AS count
FROM information_schema.tables
WHERE table_schema = '$database’
AND table_name = '$tablename’
“);
return mysql_result($res, 0) == 1;
}
?>