我正在使用我在网上找到的修改后的 php mysql 备份脚本来备份我的 sql 数据库,但我一直遇到 mysql_fetch_row 的问题,我想知道是否有办法修复它。
我用内存错误评论了这一行。
<?php
ini_set('memory_limit','4000M');
$ho = "host";
$us = "username";
$pa = "password";
$da='dbname';
backup_tables($ho,$us,$pa,$da);
/* backup the db OR just a table */
function backup_tables($host,$user,$pass,$name,$tables = '*')
{
$link = mysql_connect($host,$user,$pass);
mysql_select_db($name,$link);
//get all of the tables
if($tables == '*')
{
$tables = array();
$result = mysql_query('SHOW TABLES');
while($row = mysql_fetch_row($result))
{
$tables[] = $row[0];
}
}
else
{
$tables = is_array($tables) ? $tables : explode(',',$tables);
}
$filename = 'db-backup-'.time().'-'.(md5(implode(',',$tables))).'.sql';
$handle = fopen($filename,'w');
//cycle through
foreach($tables as $table)
{
$result = mysql_query('SELECT * FROM '.$table);
$num_fields = mysql_num_fields($result);
fwrite( $handle, 'DROP TABLE '.$table.';' );
$row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table));
fwrite( $handle, "\n\n".$row2[1].";\n\n" );
for ($i = 0; $i < $num_fields; $i++)
{
//this is the line with the error******
while($row = mysql_fetch_row($result))
{
fwrite( $handle, 'INSERT INTO '.$table.' VALUES(' );
for($j=0; $j<$num_fields; $j++)
{
$row[$j] = addslashes($row[$j]);
$row[$j] = ereg_replace("\n","\\n",$row[$j]);
if (isset($row[$j])) { fwrite( $handle, '"'.$row[$j].'"' ) ; }
else { fwrite( $handle, '""' ); }
if ($j<($num_fields-1)) { fwrite( $handle, ',' ); }
}
fwrite( $handle, ");\n" );
}
}
fwrite( $handle, "\n\n\n" );
}
//save file
fclose($handle);
}
?>
这是错误:
Fatal error: Out of memory (allocated 1338507264) (tried to allocate 35 bytes) in /home/user/backupSQL/backupmodified.php on line 47
我知道有更好的方法来进行备份,但这满足了我对系统的所有要求,而且我只遇到了我可笑的庞大数据库的内存问题。
谢谢您的帮助。
-PHP/MySQL 新手
附言。这是我使用的链接http://www.htmlcenter.com/blog/back-up-your-mysql-database-with-php/
编辑:mysqldump 在备份这些大型数据库时工作正常,但大型数据库是修改最多的数据库,当有人需要处理它们时,我不能在转储时锁定我的 dbs。这就是我使用这个脚本的原因。