I have experienced about two times that mysql database crashes and it can not be connected for a long time and I always have to contact the hosting service to reactivate it…for some reason. So I want to create a mechanism to resolve this problem whit the backup file. heres the code:
<?php
$query = //some source
$db_user = "data_search";
$db_password = "password";
$database = "data_search";
$backupfile = '/data_search.sql';
$db_table = "words";
$db = mysql_connect($hostname, $db_user, $db_password);
mysql_select_db($database,$db);
mysql_query("set names 'utf8'");
if ($db) {
$result = mysql_query("
SELECT
*
FROM
$db_table
WHERE
word = '$query'
LIMIT
0 , 1
");
} else {
$result = mysql_query("
LOAD DATA INFILE
'$backupfile'
FROM
'$db_table'
WHERE
word = '$query'
LIMIT
0 , 1
");
}
$num = mysql_numrows($result);
$i = 0;
while ($i < $num) {
$word = mysql_result($result,$i,"word");
echo $word;
$i++;
}
Now this is only a sample and im 100% accurate that the error is in the backup query($result). Basically what I want is to: if it cant connect to use the backup file. This returns $num = 0; Can someone help me to execute this code? What mysql_query() am I looking for?
Use
mysqldumpinstead. You may start it from PHP:Or just do that via a shell-script: