This is an attempt to create an backup script for my online databases. There
are few things to sort out and the coding is not the most effiecient out
there yet!!!

I just wanted to check on what you guys think, am I going the right way, is
there anything I should keep in mind?

Mario

Code below
-----------------
<?php 
$host = "localhost";
$user = "xxxxx";
$password = "xxxxx";
$database = "xxxxxx";
$field = "xxxxxxx";

$db_link = mysql_connect($host, $user, $password) or die ("error connect");
mysql_select_db($database,$db_link);
$query="DESC $field";
$results=mysql_query($query);

        while ($row=mysql_fetch_array($results)) {

                if ($row[3]=="PRI") {
                        $thepri = $row[0];
                }       
                        $thesql .= $row[0] . " " . $row[1];     
                        if ($row[2]=="YES") {
                                $thesql .= " default NULL";
                        } else {
                                $thesql .= " NOT NULL";
                        }
                        $thesql .= " " . $row[5];
                        $thesql .= ", \n";              
        }
$thecreate = "CREATE TABLE " . $field . "_backup ( \n";         
$thecreate .= $thesql;
$thecreate .= " PRIMARY KEY  (" . $thepri . ")\n";
$thecreate .= ") TYPE=MyISAM;";
echo "The CREATE STATEMENT<br>";
echo $thecreate;
echo "<br>";
echo "<br>";

//==============================//
//======== INSERT CODE ==========//
//==============================//

        $result = mysql_list_tables($database);
            
            if (!$result) {
                print "DB Error, could not list tables\n";
                print 'MySQL Error: ' . mysql_error();
                exit;
            }
            $z=1;
            while ($row = mysql_fetch_row($result)) {
                //***********************
               //IMPROVEEEEEEEEE
               //echo $row[0] . "\n<br>";
               $z++;
            }
//****************************************
//MAKE THE FOLLOWING DYNAMIC

$query2 = "SELECT * from " . $field;
$results=mysql_query($query2);

$m=0;
while ($row=mysql_fetch_array($results)) {
        
        
        for ($i=0;$i<=$z-1;$i++) {
                $x = $row[$i];
                if (($i == 0) && ($m<>0)) {
                        $therest .=  "),";
                }
                if ($i == 0) {
                        $therest .=  "(";
                }
                
                if (!isset($x)) { 
                        $x = "NULL";
                } else {
                        $x = "'" . addslashes($x). "'";
                }
                
                if ($i<>0) {
                        $therest .= ",";
                }
                
                $therest .= $x; 
        }
$m++;
}
$therest .=  ")";


echo "The INSERT STATEMENT<br>";
$theinsert =  "INSERT INTO ". $field . "_backup VALUES ";
$theinsert .= $therest . ";";
echo $theinsert;
?>

Reply via email to