Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8424023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:52:43+00:00 2026-06-10T03:52:43+00:00

I’m using a modified php mysql backup script I found on the net to

  • 0

I’m using a modified php mysql backup script I found on the net to backup my sql databases but I kept running into an issue with mysql_fetch_row and I was wondering if there was a way to fix it.

I commented the line with the memory error.

<?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);
}
?>

and this is the error:

Fatal error: Out of memory (allocated 1338507264) (tried to allocate 35 bytes) in /home/user/backupSQL/backupmodified.php on line 47

I know there are better ways to do a backup but this meets all my requirements for my system and I only run into the memory problem with my ridiculously huge databases.

Thanks for the help.

-PHP/MySQL newbie

ps. heres the link i used http://www.htmlcenter.com/blog/back-up-your-mysql-database-with-php/

edit: mysqldump works fine when backing up these large databases, but the large dbs are the most modified ones and I cant have my dbs lock while dumping when someone needs to work on them. That’s why I resorted to this script.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T03:52:44+00:00Added an answer on June 10, 2026 at 3:52 am

    As the other posters have mentioned, you would be better off using a ready-made tool, but let’s talk about your script for now.

    If you have a ridiculously large table, then there’s just no way to allocate that much memory. You’ll have to split your data set into multiple chunks. Use a query like:

    $result = mysql_query('SELECT * FROM '.$table.' LIMIT '.$start.', 10000');
    

    Then just iterate with $start or do a while loop to check if you’re still getting results. Like this, you’ll get the data in chunks of 10000 records so it’ll surely fit in your memory.

    There’s still a problem, though – consistency. What if some rows change while you’re still fetching chunks? Well, you could wrap your SELECT loop in a transaction and lock the table while you’re getting the data. I know you’re trying to avoid locks, but locking a table is better than an entire database. This still won’t ensure inter-table consistency, but it’s the best that can be done under the conditions you set.

    EDIT: Here is the code for the loop. I implemented the while solution. There is an equivalent for loop, but the comparison between the two is outside the scope of this post.

    foreach($tables as $table)
    {
        fwrite( $handle, 'DROP TABLE '.$table.';' );
    
        $row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table));
        fwrite( $handle, "\n\n".$row2[1].";\n\n" );
    
        $start = 0;
        do {
            $result = mysql_query( 'SELECT * FROM '.$table.' LIMIT '.$start.', 10000' );
            $start += 10000;
            $num_rows = mysql_num_rows( $result );
    
            while( $row = mysql_fetch_row( $result ) ) {
                $line = 'INSERT INTO '.$table.' VALUES(';
                foreach( $row as $value ) {
                    $value = addslashes( $value );
                    $value = ereg_replace( "\n","\\n", $value );
                    $line .= '"'.$value.'",';
                }
                $line = substr( $line, 0, -1 ); // cut the final ','
                $line .= ');\n';
                fwrite( $handle, $line );
            }
        } while( $num_rows !== 0 );
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am currently running into a problem where an element is coming back from
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I would like to count the length of a string with PHP. The string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.