如何解决php中备份文件的问题
我经历过大约两次mysql数据库崩溃并且长时间无法连接并且我总是必须联系托管服务来重新激活它......出于某种原因。所以我想创建一个机制来解决备份文件的这个问题。代码如下:
<?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++;
}
现在这只是一个示例,我 100% 准确地知道错误出现在备份 query($result)
中。基本上我想要的是:如果无法连接以使用备份文件。这将返回 $num = 0;
有人可以帮我执行这段代码吗?我在寻找什么mysql_query()
?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
请改用 mysqldump。您可以从 PHP 启动它:
或者通过 shell 脚本来启动它:
Use
mysqldump
instead. You may start it from PHP:Or just do that via a shell-script:
您可以使用MySQLDumper(它与mysqldump不同!)您可以对其进行编程以通过FTP或每封电子邮件,这样当服务器完全崩溃时他们就不会丢失......
You can use MySQLDumper (it is not the same as mysqldump!) You can program it to transfer DBs through FTP or per email so they don't get lost when the server crashes completely...
我还建议您使用 mysqldump,但如果您有 shell 访问权限,我建议您直接执行此操作,而不必处理 php 超时和内存问题。
例如,如果您希望定期进行备份,那么您应该通过运行 bash 脚本的 cron 来执行此操作。
如果我们知道此脚本的用例,将会有所帮助。
I also suggest you use mysqldump, but I recommend you do this directly if you have shell access instead of having to deal with php timeouts and memory issues.
If for instance you are hoping to make regular backups then you should do this via a cron that runs a bash script.
It would help if we knew the use case for this script.