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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:15:14+00:00 2026-06-17T22:15:14+00:00

I would like to backup tables (with PHP) from a db if the table

  • 0

I would like to backup tables (with PHP) from a db if the table prefix is matching with a sub string. What I was trying and is not working

error_reporting(1);
$dbname = 'wp_dev';

if (!mysql_connect('127.0.0.1', 'root', '')) {
    echo 'Connection Error';
    exit;
}

$sql = "SHOW TABLES FROM $dbname LIKE 'wp_%'";
$result = mysql_query($sql);

if (!$result) {
    echo "DB tables could not be listed\n";
    echo 'MySQL Fehler: ' . mysql_error();
    exit;
}

while ($row = mysql_fetch_row($result)) {
    echo "<pre>Table: {$row[0]}\n</pre>";
    system( 'mysqldump $dbname $row[0] > verlag_$row[0].sql');
}

mysql_free_result($result);
  • 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-17T22:15:15+00:00Added an answer on June 17, 2026 at 10:15 pm

    Here is a function for making bakups from db or only some tables

    function &backup_tables($host, $user, $pass, $name, $tables = '*'){
      $data = "\n/*---------------------------------------------------------------".
              "\n  SQL DB BACKUP ".date("d.m.Y H:i")." ".
              "\n  HOST: {$host}".
              "\n  DATABASE: {$name}".
              "\n  TABLES: {$tables}".
              "\n  ---------------------------------------------------------------*/\n";
      $link = mysql_connect($host,$user,$pass);
      mysql_select_db($name,$link);
      mysql_query( "SET NAMES `utf8` COLLATE `utf8_general_ci`" , $link ); // Unicode
    
      if($tables == '*'){ //get all of the 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);
      }
    
      foreach($tables as $table){
        $data.= "\n/*---------------------------------------------------------------".
                "\n  TABLE: `{$table}`".
                "\n  ---------------------------------------------------------------*/\n";           
        $data.= "DROP TABLE IF EXISTS `{$table}`;\n";
        $res = mysql_query("SHOW CREATE TABLE `{$table}`", $link);
        $row = mysql_fetch_row($res);
        $data.= $row[1].";\n";
    
        $result = mysql_query("SELECT * FROM `{$table}`", $link);
        $num_rows = mysql_num_rows($result);    
    
        if($num_rows>0){
          $vals = Array(); $z=0;
          for($i=0; $i<$num_rows; $i++){
            $items = mysql_fetch_row($result);
            $vals[$z]="(";
            for($j=0; $j<count($items); $j++){
              if (isset($items[$j])) { $vals[$z].= "'".mysql_real_escape_string( $items[$j], $link )."'"; } else { $vals[$z].= "NULL"; }
              if ($j<(count($items)-1)){ $vals[$z].= ","; }
            }
            $vals[$z].= ")"; $z++;
          }
          $data.= "INSERT INTO `{$table}` VALUES ";      
          $data .= "  ".implode(";\nINSERT INTO `{$table}` VALUES ", $vals).";\n";
        }
      }
      mysql_close( $link );
      return $data;
    }
    

    How to use:

    // create backup
    //////////////////////////////////////
    
    $backup_file = 'db-backup-'.time().'.sql';
    
    // get backup
    $mybackup = backup_tables("myhost","mydbuser","mydbpasswd","mydatabase","*");
    
    // save to file
    $handle = fopen($backup_file,'w+');
    fwrite($handle,$mybackup);
    fclose($handle);
    

    You can modify the line:

    $result = mysql_query(“SHOW TABLES”);

    for the table präfix

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

Sidebar

Related Questions

I would like to know why is this: backup.sql file not being accepted by
I'm currently using a script from http://davidwalsh.name/backup-mysql-database-php to do a hotcopy like backup of
I would like to know how people tackle backup and restore of data in
We would like to use a cronjob to create a database backup. The backup
I would like to know the on disk format of an RMAN backup file.
Would like to parse IPv4 address from exit-addresses . Format of the file: ExitNode
Would like to make anapplication in Java that will not automatically parse parameters used
I do not wish to prevent table drops, but when certain tables are dropped
I am currently working on a little backup script from some firebird databases and
I would like to use rebol to download an xml backup of my blog

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.