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

  • Home
  • SEARCH
  • 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 945999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:50:42+00:00 2026-05-15T22:50:42+00:00

I am using the Database Backup script by David Walsh( http://davidwalsh.name/backup-mysql-database-php ) to backup

  • 0

I am using the Database Backup script by David Walsh(http://davidwalsh.name/backup-mysql-database-php) to backup my MYSQL database as a .sql file to my server.

I created a user named backup and gave it all privileges(just to make sure). Then I put the code into a php file and setup a cron job to run the php file.

This is the code:

/* 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);
    }

    //cycle through
    foreach($tables as $table)
    {
        $result = mysql_query('SELECT * FROM '.$table);
        $num_fields = mysql_num_fields($result);

        $return.= 'DROP TABLE '.$table.';';
        $row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table));
        $return.= "\n\n".$row2[1].";\n\n";

        for ($i = 0; $i < $num_fields; $i++) 
        {
            while($row = mysql_fetch_row($result))
            {
                $return.= '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])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; }
                    if ($j<($num_fields-1)) { $return.= ','; }
                }
                $return.= ");\n";
            }
        }
        $return.="\n\n\n";
    }

    //save file
    $handle = fopen('../backup/db-backup-'.time().'-'.(md5(implode(',',$tables))).'.sql','w+');
    fwrite($handle,$return);
    fclose($handle);
}

backup_tables('localhost','alupto_backup','pass','*');

When the cron job runs, the backup does not work and I receive an email with the error that appears:

Warning:
mysql_fetch_row(): supplied argument
is not a valid MySQL result resource
in
/home5/ideapale/public_html/amatorders_basic/admin/backup.php
on line 18

On line 18 is this code:

while($row = mysql_fetch_row($result))

When I run the SQL(SHOW TABLES) in phpMyAdmin, it works fine and shows me a list of all the tables. But for some reason, I am receiving an error when the php file tries to run the SQL.

Why is my database backup script not working?

  • 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-05-15T22:50:43+00:00Added an answer on May 15, 2026 at 10:50 pm

    This will not work to back up your database as an SQL script, unless your database is just a toy database, the equivalent of a “hello world” script.

    That script is appalling. You should not use it to back up a database. That script has been posted before: PHP Database Dump Script – are there any issues?

    • No error checking after mysql_connect() or mysql_queries(). You probably just gave a wrong password or something, but you’d never know because the script doesn’t verify the connect was successful.

    • It won’t produce the right INSERT statement if your database contains any NULLs.

    • Character sets are not handled.

    • addslashes() is not suitable for escaping data.

    • Table names are not delimited.

    • Does not back up views, procedures, functions, or triggers.

    • mysql_query() buffers results, so if you have a table with thousands of rows or more, you’ll exceed your PHP memory limit. In fact, the script concatenates the series of INSERT statements into a single PHP variable. So before it finishes, you will have your entire database represented in memory.

    No one should ever use that script. It’s utter garbage, and I do not say that lightly.

    Just use shellexec() to run mysqldump.

    @Álvaro G. Vicario has a good point, there’s no need for you to even use PHP for this task. I was assuming you need to make a backup from a PHP script. Here’s how I would create a backup from a cron script:

    Create a shell script, it can be called whatever you want, e.g. mymysqldump.sh. Here’s how I would write it:

    :
    : ${BACKUP_HOST:="localhost"}
    : ${BACKUP_DATABASE:="mydatabase"}
    : ${BACKUP_DIR:="/opt/local/var/db/mysql5/backups"}
    : ${BACKUP_FILE:="${DATABASE}-`date +%Y%m%d%H%M%S`"}
    
    mysqldump -h ${BACKUP_HOST} ${BACKUP_DATABASE} > ${BACKUP_DIR}/${BACKUP_FILE}
    

    Of course customize the values of the variables as needed for your environment.

    You may notice that the username and password are not in this file. Please don’t put passwords into scripts in plain text so everyone can read them. Instead, we’ll put them in an options file to make it more secure.

    Create a special operating system user who is going to run the backup from cron. Your system may have a special user “mysql” or “_mysql” to run the MySQL Server, but this user may be configured to have no valid home directory. You need a user that has a home directory. Let’s call it “mybackup”.

    Under that user’s home directory, create a file .my.cnf with the following content:

    [mysqldump]
    user = alupto_backup
    password = xyzzy
    

    Where “alupto_backup” and “xyzzy” are the MySQL username and its password (change these for your environment). Set the ownership and mode of this file so that only its owner can read it:

    chown mybackup .my.cnf
    chmod 600 .my.cnf
    

    Create a bin directory under this user’s home and put our shell script into it.

    mkdir ~mybackup/bin
    mv mymysqldump ~mybackup/bin
    

    Now you can run the shell script to test it:

    sh ~mybackup/bin/mymysqldump
    

    Now create a cron file for this user:

    crontab -u mybackup
    
    @daily ~mybackup/bin/mymysqldump
    

    That should be it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 523k
  • Answers 523k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Other answers have given all the good choices, but without… May 16, 2026 at 9:38 pm
  • Editorial Team
    Editorial Team added an answer The INSERT syntax doesn't allow for aliases. Why would you… May 16, 2026 at 9:38 pm
  • Editorial Team
    Editorial Team added an answer BorderContainer has a minimum size of 112 pixels wide by… May 16, 2026 at 9:38 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

When I backup or restore a database using MS SQL Server Management Studio, I
I am using phpadmin and I exported a .sql file to backup my database.
I'm using a hosting service which allows me to backup my SQL 2008 database
We have customers who, for unassailable reasons, cannot use SQL Server's built-in backup features
I've got a problem trying to build a easy backup/upgrade database script. The error
I have .net app in which there is function for Database backup and restore.
I'm trying to transfer an entire column's worth of data from the backup database
I've backup from a sqlserver created with this procedure: protected void WriteXML(string tableName) {
I need to do some structural changes to a database (alter tables, add new
Is it possible to see the DML (SQL Statement) that is being run that

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.