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 6901525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:41:05+00:00 2026-05-27T07:41:05+00:00

Ok so my web host doesn’t allow the use of INTO OUTFILE / mysql

  • 0

Ok so my web host doesn’t allow the use of INTO OUTFILE / mysql dump and I am required to develop a backup script.

How would I go about doing this in PHP. Thanks

  • 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-27T07:41:06+00:00Added an answer on May 27, 2026 at 7:41 am

    There are some nifty and still simple backup scripts out there; the one from PHPMyAdmin is probably a very good one because well tested, the problem is to extract that from the whole package to be able to call it automatically.

    For the exact same task I developed my own small script, which iterates over all tables in a database and sends the contents in a bzipped sql file (meaning that it can easily be imported again if the backup should be needed).

    Because it is so simple, this script of course also has its limitations, as pointed out in the comment below: It only backs up the table content. Not even the table structure is preserved, the assumption here is that the table structure changes very seldomly and also could be easily reconstructed if needed. But if your software changes the schema rather often and automatically, or if it uses functions or views, you might have to look for a more advanced script!

    So far for the “disclaimers”, here we go with the simple script:

    <?php
    $db_host = "localhost";
    $db_name = $_GET["db"];
    $db_user = $_GET["user"];
    $db_pass = $_GET["pass"];
    $do_truncate = $_GET["truncate"];
    
    function datadump ($table, $do_truncate)
    {
        $result .= "-- -------- TABLE '$table' ----------\n";
        $query = mysql_query("SELECT * FROM ".$table);
        $numrow = mysql_num_rows($query);
        $numfields = mysql_num_fields($query);
        if ($numrow > 0)
        {
            if (strcmp($do_truncate, "yes") == 0)
            {
                $result .= "TRUNCATE TABLE ".$table.";\n";
            }
            $result .= "INSERT INTO `".$table."` (";
            $i = 0;
            for($k=0; $k<$numfields; $k++ )
            {
                $result .= "`".mysql_field_name($query, $k)."`";
                if ($k < ($numfields-1))
                {
                    $result .= ", ";
                }
            }
            $result .= ") VALUES ";
            while ($row = mysql_fetch_row($query))
            {
                $result .= " (";
                for($j=0; $j<$numfields; $j++)
                {
                    if (mysql_field_type($query, $j) == "string" ||
                        mysql_field_type($query, $j) == "timestamp" ||
                        mysql_field_type($query, $j) == "time" ||
                        mysql_field_type($query, $j) == "datetime" ||
                        mysql_field_type($query, $j) == "blob")
                    {
                        $row[$j] = addslashes($row[$j]);
                        $row[$j] = ereg_replace("\n","\\n",$row[$j]);
                        $row[$j] = ereg_replace("\r","",$row[$j]);
                        $result .= "'$row[$j]'";
                    }
                    else if (is_null($row[$j]))
                    {
                        $result .= "NULL";
                    else
                    {
                        $result .= $row[$j];
                    }
                    if ( $j<($numfields-1))
                    {
                        $result .= ", ";
                    }
                }
                $result .= ")";
                $i++;
                if ($i < $numrow)
                {
                    $result .= ",";
                }
                else
                {
                    $result .= ";";
                }
                $result .= "\n";
            }
        }
        else
        {
            $result .= "-- table is empty";
        }
        return $result . "\n\n";
    }
    
    
    mysql_connect($db_host,$db_user,$db_pass);
    @mysql_select_db($db_name) or die("Unable to select database.");
    $tableQ = mysql_list_tables ($db_name);
    $i = 0;
    $content  =  "-- --------------------------------\n";
    $content .=  "-- DATABASE DUMP\n";
    $content .=  "-- DATE: ".date("d-M-Y")."\n";
    $content .=  "-- DB NAME: ".$db_name."\n";
    $content .=  "-- TRUNCATE: ".((strcmp($do_truncate, "yes") == 0)? "YES" : "NO")."\n";
    $content .=  "-- ---------------------------------\n\n";
    while ($i < mysql_num_rows ($tableQ))
    {
        $tb_names[$i] = mysql_tablename ($tableQ, $i);
        $content .= datadump($tb_names[$i], $do_truncate);
        $i++;
    }
    $file_name = "MySQL_Database_Backup.sql.bz2";
    Header("Content-type: application/octet-stream");
    Header("Content-Disposition: attachment; filename=$file_name");
    echo(bzcompress(utf8_encode($content)));
    exit;
    ?>
    

    I guess it still could need a few improvements (security e.g.), but it serves the purpose quite well. To use it, upload it to your webspace, let’s say as ‘backup-db.php’ (preferredly in some hidden folder), and call it like this

    http://yoursite/backup-db.php?host=yourdbhost&name=<yourdbname>&user=<dbuser>&pass=<dbpass>&truncate=<whethertoaddtruncatestatementforeachtable>
    

    You of course first have to adapt the values in brackets <>. Feel free to use and adapt it; should you make any improvements, I’d be very glad to hear about it!

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

Sidebar

Related Questions

I am using a free web host that doesn't support back end code. I
I have a cron script on a shared web host that occasionally gets killed.
I just rolled a small web application into production on a remote host. After
I am getting a web host and i have projects with teammats. I thought
123-reg are my web host and they dont have a control panel to convert
I have a Drupal site on a shared web host, and it's getting a
After I upload my PHP files to my web host, I view the page
I'm trying to install the Recess PHP framework on my web host (Dreamhost). It
I write and host web applications on Windows servers for intranet usage. My server
Scenario Multiple application servers host web services written in Java, running in SpringSource dm

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.