# 5 Re: Text file database
No. SQL is a command language that works directly with a SQL based server.
Hmmm if thats the case is there anyway to copy the contents of a database to a back up file so it can be moved to another location?. The code i am using is as follows if it helps
<?php
$sql = $_POST['sql'];
$link = mysql_connect('localhost', 'dbname', 'password');
if (!$link) {
die('0:' . mysql_error());
}
if (!mysql_select_db('dbName', $link)) {
echo '0: Could not select database';
exit;
}
$result = mysql_query($sql, $link);
if (!$result) {
echo "0: could not query the database\n";
echo mysql_error();
exit;
}
else
{
$row=mysql_fetch_array($result);
echo $row[0];
mysql_free_result($result);
}
mysql_close($link);
?>
# 6 Re: Text file database
Hmmm if thats the case is there anyway to copy the contents of a database to a back up file so it can be moved to another location?
Well, I usually don't like to share something of the following nature but here is the full source code to one of my own MySQL backup PHP pages. It will connect to MySQL and output the complete MySQL database to a PHP file. That PHP file, when run, will be exactly the same as the old MySQL database.
EDIT: INSTRUCTIONS...
1. Put this on your webserver.
2. Run it from a URL. (http://localhost/backup.php)
3. It compiles a PHP file of the whole database in the same directory as that of backup.php.
4. Now that PHP file can be put on a webserver and run from a URL.
5. When executed, it makes that MySQL server identical to the one it came from.
<?php
$date = date(".Ymd.Hi");
$file = "bckp" . $date . ".php";
$output = fopen($file, 'w');
$user = ""; // Put MySQL username here
$pass = ""; // Put MySQL password here
fwrite($output, "<?php\r\n\r\n");
fwrite($output, "\$user = '$user';\r\n");
fwrite($output, "\$pass = '$pass';\r\n");
fwrite($output, "\$connection = mysql_connect('localhost', \$user, \$pass);\r\n\r\n");
$connection = mysql_connect('localhost', $user, $pass);
$dblist = mysql_list_dbs($connection);
while($row = mysql_fetch_object($dblist)){
$db = $row->Database;
$dbcreate = "mysql_query(\"CREATE DATABASE `$db`\");";
fwrite($output, $dbcreate . "\r\n");
$dbselect = mysql_select_db('$db');
fwrite($output, "mysql_select_db('$db');\r\n");
$tbl = mysql_list_tables($db);
while($table = mysql_fetch_row($tbl)){
$fld = mysql_query("SELECT * FROM `$table[0]`");
$fields = mysql_num_fields($fld);
unset($tblcreate);
$tblcreate = "CREATE TABLE `$table[0]` (";
$arrfield = array();
$arrfieldnum = 0;
for($i=0;$i<$fields;$i++){
$fieldinfo = mysql_fetch_field($fld, $i);
unset($fieldname);
$fieldname = $fieldinfo->name;
$arrfield[$arrfieldnum] = $fieldname;
$arrfieldnum++;
$fieldlen = mysql_field_len($fld, $i);
if($fieldlen=="65535"){$fieldtype = "TEXT";}
else{$fieldtype = "VARCHAR($fieldlen)";}
$tblcreate .= "`$fieldname` " . $fieldtype . " NOT NULL";
if($i !== $fields - 1){$tblcreate .= ", ";}
}
$tblcreate .= ")";
fwrite($output, " mysql_query(\"" . $tblcreate . "\", \$connection);\r\n");
$readtable = "SELECT * FROM `$table[0]`";
$retval = mysql_query($readtable);
while($col = mysql_fetch_row($retval)){
$insert = "mysql_query(\"INSERT INTO `$table[0]` (";
for($ifield=0;$ifield<count($arrfield)-1;$ifield++){
$insert .= "`" . $arrfield[$ifield] . "`, ";
}
$insert .= "`" . $arrfield[$ifield] . "`) VALUES (";
for($icol=0;$icol<count($col)-1;$icol++){
$col[$icol] = str_replace("\"", "'", $col[$icol]);
$insert .= "'" . $col[$icol] . "', ";
}
$insert .= "'" . $col[$icol] . "'";
$insert .= ")\", \$connection);";
fwrite($output, " $insert\r\n");
}
}
}
mysql_close($connection);
fwrite($output, "\r\nmysql_close(\$connection);\r\n\r\n");
fwrite($output, "?>");
fclose($output);
?>
<html>
<title>MySQL Backup</title>
<body>
<font face="arial" size=5 color="#000000"><b>Backup Complete!</b></font>
</body>
</html>