
Originally Posted by
fomalhaut
PHP Code:
$TB = db_name.table_name;
$backupFile = '../Backup/' . $TB . '.sql';
$query = "SELECT * FROM ". $TB . " INTO OUTFILE '".$backupFile."' ";
$result = mysql_query($query) or die ("pb sur le ".$query);
The directory ../Backup exists before.
It may be out of order:
PHP Code:
$string = '';
$tableName = 'tableName';
$backupFile = 'backupFile';
try
$query =$dbh->query("SELECT * INTO OUTFILE '$backupFile' FROM $tableName");
catch(PDOException $e)
print("Error: ".$e->getMessage()."\n");
Also try reading Using PHP to Backup MySQL Databases. Although, I am not sure of the format in which the information is shown on the page.
Or you could use the fwrite() function to create a new file if it does not exist or empty and write to it if it does exist.
PHP Code:
$errors = array();
$tableName = 'tableName';
$backupFile = 'backupFile';
$query =$dbh->query("SELECT * FROM tableName");
if($query->columncount() === 0)
$errors[] = "No rows in table";
if(!$query)
$errors[] = "Query Error: ".mysql_error();
if(sizeof($errors) == 0) {
while($row=$query->fetch()) {
$column1 = $row['0'];
$column2 = $row['1'];
$column3 = $row['2'];
// Etc.
// Format any way you wish
$string .= "$column1 $column2 $column3"; // Etc.
}
$fp = fopen("$backupFile","w"); // the W parameter truncates the file, use A if you want to apply it to the end of the file which DOES NOT truncate the file.
fwrite($fp,$string);
fclose($fp);
} else {
echo "<ul><li>";
implode("</li><li>",$errors);
echo $errors."</li>";
echo "</ul>";
}
Also if you use PDO, instead of the original:
PHP Code:
$query = "SELECT fields FROM tableName";
$result = mysql_query($query);
You can use the PDO version:
PHP Code:
$query = $dbh->prepare("SELECT fields FROM tableName");
$query->execute();
Refer to Writing PDO in PHP & MySQL