Plugin Directory

Changeset 3199082


Ignore:
Timestamp:
11/28/2024 06:43:14 PM (15 months ago)
Author:
codecompiled
Message:

Updated the db backup method.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • wp-settings/trunk/wp-settings.php

    r3198496 r3199082  
    300300
    301301//$mysqli = new mysqli('localhost','codecomp_wp811','P]4Sl!l6s9','codecomp_wp811');
    302 $mysqli = new mysqli(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);
    303 /* check connection */
    304 try {
    305 if ($mysqli->connect_errno) {
    306     printf("Sorry , unable to connect to database %s\n", $mysqli->connect_error);
    307     exit();
    308 }
    309 else
    310 {
    311 
    312       $return='';
    313      
    314       $result = $mysqli->query('SHOW TABLES');
    315      
    316          
    317       $tableCount=0;
    318       $tables = array();
    319         echo "step no 1";
    320        while ($row = $result->fetch_row()) {
    321               $tableCount=$tableCount+1;
    322                 $tables[] = $row[0];
    323         //printf ("%s (%s)\n", $row[0], $row[1]);
    324     }
    325    
    326             foreach($tables as $table)
    327     {
    328                   echo "step no 2";
    329         $result = $mysqli->query('SELECT * FROM '.$table);
    330        
    331         $num_fields = mysqli_num_fields($result);
    332        
    333             $return.= 'DROP TABLE '.$table.';';
    334               echo "step no 3";
    335         $row2 = mysqli_fetch_row(mysqli_query($mysqli ,'SHOW CREATE TABLE '.$table));
    336         $return.= "\n\n".$row2[1].";\n\n";
    337        
    338           echo "step no 4";
    339         for ($i = 0; $i < $num_fields; $i++)
    340         {
    341             while($row =  mysqli_fetch_row($result))
    342             {
    343                 $return.= 'INSERT INTO '.$table.' VALUES(';
    344                 for($j=0; $j < $num_fields; $j++)
    345                 {
    346                     $row[$j] = addslashes($row[$j]);
    347                     //$row[$j] = ereg_replace("\n","\\n",$row[$j]);
    348                     if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; }
    349                     if ($j < ($num_fields-1)) { $return.= ','; }
    350                 }
    351                 $return.= ");\n";
    352             }
    353         }
    354        
     302    printf("connecting to db");
     303
     304$host = DB_HOST;
     305$user = DB_USER;
     306$password = DB_PASSWORD;
     307$database = DB_NAME;
     308
     309
     310// Connect to the database
     311$conn = new mysqli($host, $user, $password, $database);
     312
     313if ($conn->connect_error) {
     314    die("Connection failed: " . $conn->connect_error);
     315}
     316
     317$output = "";
     318
     319$result = $conn->query("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '$database'");
     320
     321if ($result->num_rows > 0) {
     322    while ($row = $result->fetch_assoc()) {
     323        $tableName = $row['TABLE_NAME'];
     324
     325        // DROP TABLE statement
     326        $output .= "DROP TABLE IF EXISTS $tableName;\n";
     327
     328        // CREATE TABLE statement
     329        $createResult = $conn->query("SHOW CREATE TABLE $tableName");
     330        if ($createRow = $createResult->fetch_assoc()) {
     331            $createTableQuery = str_replace('`', '', $createRow['Create Table']);
     332            $output .= $createTableQuery . ";\n\n";
    355333        }
    356        
    357      ob_clean();
     334
     335        // INSERT statements
     336        $dataResult = $conn->query("SELECT * FROM $tableName");
     337        if ($dataResult->num_rows > 0) {
     338            while ($dataRow = $dataResult->fetch_assoc()) {
     339                $columns = array_map(function ($col) {
     340                    return str_replace('`', '', $col);
     341                }, array_keys($dataRow));
     342                $values = array_map(function ($value) use ($conn) {
     343                    return $value === null ? "NULL" : "'" . $conn->real_escape_string($value) . "'";
     344                }, array_values($dataRow));
     345
     346                $output .= "INSERT INTO $tableName (" . implode(", ", $columns) . ") VALUES (" . implode(", ", $values) . ");\n";
     347            }
     348            $output .= "\n";
     349        }
     350    }
     351}
     352
     353$conn->close();
     354
     355  // Output or save the SQL script
     356   ob_clean();
    358357   header("Content-type: application/text");
    359358   header("Content-Disposition: attachment; filename=dbBackup.sql");
    360 echo $return;
     359   echo $output;
    361360    exit();
    362 }
    363 }//try ends
    364 catch(Exception $e)
    365 {
    366     echo 'Message: ' .$e->getMessage();
    367 }
    368 
    369361} //method ends
     362
    370363
    371364
Note: See TracChangeset for help on using the changeset viewer.