| px | top | add code | search | signup | login | help |
<?php
//Here are your variables, set them for your database
$dbUser=""; //User Name
$dbUserPw=""; //Password
$dbName="wardrox_com1"; //db Name
$dbHost="localhost"; //Host Name
function run_query($query) {
//Run a query on the database, this function is very very usefull
//taken from "Beginning PHP" - exelent book
global $dbHost, $dbUser, $dbUserPw, $dbName;
$link = mysql_connect($dbHost, $dbUser, $dbUserPw)
or die("Could not connect : " . mysql_error());
mysql_select_db($dbName) or die("Could not select database");
$temp = mysql_query($query) or die("Query failed : " . mysql_error());
Return $temp;
}
function echo_backup($tablename){
//function created by wdx - wardrox.com - (C) 2005
//You may use this code as you wish, however i do ask that you keep
//this message in the code. If you want any help feel free to email me
//thanks,
//Please be aware I am not a programmer by profession, its just a hobby.
//wdx@wardrox.com
//Whats E.T. short for? He's got small legs.
//this function will iterate through your table of choice and generate a query
//which if run will restore your database.
//First run a query to get a single row of data, to ge tthe header iformation from
$query = "SELECT * FROM $tablename";
$result = run_query($query);
$result_assoc = mysql_fetch_assoc($result);
//fisrt create headers for the tables tablesand echo them
$output .= "REPLACE INTO $tablename (";
while (list($Header) = each($result_assoc)) {
//stepping through each collumn header in the table
$result_head[] .= $Header;
}
$result_head = implode(", ", $result_head);
//ouput the header
$output .= $result_head . ") <br> VALUES <br>";
//Once the table headers have been displayed then start on the actuall data
while ($data_details = mysql_fetch_assoc($result)) {
//step through each row in the table
while (list($data_details2) = each($data_details)) {
//step through each cell in each row
//take out anything which may cause an error
$data_details[$data_details2] = htmlentities($data_details[$data_details2]);
$data_details[$data_details2] = addslashes($data_details[$data_details2]);
}
$data_backup[] = implode(", ", $data_details);
//bring together the data for each row adding a comma between each peice of information
}
$data_backup = implode("), <br><br>(", $data_backup);
$data_backup = "(" . $data_backup . ")";
//output the backup information
$output .= $data_backup . "<br><br><hr>";
return $output;
}
echo"<h1>Table Backup</h1><br>Code created by WDX - wardrox.com - Feel free to use this code as you wish, but please leave this copywrite notice in the code.<br><br><hr>";
//the function below when run will output a query string which can be usd as a backup for the specified table
//You can then either output the string or send it to anothe rlocation as the message of an email.
//The string returned can be fed into a database as a query
$output = echo_backup('TableNameHere');
echo $output;
?>
Comments or questions?
PX is running PHP 5.2.11
Thanks to Miranda Productions for hosting and bandwidth.
Use of any code from PX is at your own risk.