Web Hosting Talk







View Full Version : MySQL backup script in PHP


Research Names
02-06-2003, 11:37 PM
I'm in the process of making a MySQL backup script but so far I get this error. I can not work out why.

Parse error: parse error in /home/xxxx/public_html/admin/backup.php on line 21

<?

// This script is copyrighted to Pullan Enterprises
// You are not allowed to sell or give away this script
// You are not allowed to remove this header

require("program.php");
$db = mysql_pconnect("$dbhost", "$dbuser", "$dbpass") or die("Could not connect");
mysql_select_db("$dbname") or die("Could not select database");
$tresult = mysql_list_tables($dbname);

while ($trow = mysql_fetch_row($tresult)) {
$sql .= "CREATE TABLE $trow[0] (\n";

$result = mysql_query("select * from $trow[0]");
$i = 0;
while ($i < mysql_num_fields($result)) {
$meta = mysql_fetch_field($result);
$name = $meta->name;
$sql .= $name;
_$sql_q = "SHOW COLUMNS FROM $trow[0] LIKE '$name'";
_ _$sql_res = mysql_query($sql_q);
_ _$row = mysql_fetch_array($sql_res);
$sql .= ' '.$row["Type"];
echo "
<pre>
blob: $meta->blob
max_length: $meta->max_length
multiple_key: $meta->multiple_key
name: $meta->name
numeric: $meta->numeric
primary_key: $meta->primary_key
table: $meta->table
type: $meta->type
unique_key: $meta->unique_key
unsigned: $meta->unsigned
zerofill: $meta->zerofill
</pre>
";
$not_null = $meta->not_null;
if ($not_null == 1) $sql .= ' NOT NULL';
if ($row["Default"]) $sql .= ' DEFAULT \''.$row["Default"]."'";
$i++;
$sql .= ', ';
}

$sql .= ");\n";
}
echo $sql;_
?>

Zikkitzo
02-06-2003, 11:58 PM
First, I thought when doing ". your suppose to use ". not '.

Research Names
02-08-2003, 12:04 AM
Updated code
Error on line 21
<?

// This script is copyrighted to Pullan Enterprises
// You are not allowed to sell or give away this script
// You are not allowed to remove this header

require("program.php");
$db = mysql_pconnect("$dbhost", "$dbuser", "$dbpass") or die("Could not connect");
mysql_select_db("$dbname") or die("Could not select database");
$tresult = mysql_list_tables($dbname);

while ($trow = mysql_fetch_row($tresult)) {
$table = $trow[0];
$sql .= "CREATE TABLE $table (\n";

$result = mysql_query("select * from $table");
$i = 0;
while ($i < mysql_num_fields($result)) {
$meta = mysql_fetch_field($result);
$name = $meta->name;
$sql .= $name;
_$sql_q = "SHOW COLUMNS FROM $table LIKE '$name'";
_ _$sql_res = mysql_query($sql_q);
_ _$row = mysql_fetch_assoc($sql_res);
$sql .= ' '.$row["Type"];
echo "
<pre>
blob: $meta->blob
max_length: $meta->max_length
multiple_key: $meta->multiple_key
name: $meta->name
numeric: $meta->numeric
primary_key: $meta->primary_key
table: $meta->table
type: $meta->type
unique_key: $meta->unique_key
unsigned: $meta->unsigned
zerofill: $meta->zerofill
</pre>
";
$not_null = $meta->not_null;
if ($not_null == 1) $sql .= ' NOT NULL';
$default = $row["Default"];
if ($default) $sql .= " DEFAULT '$default'";
$i++;
$sql .= ', ';
}

$sql .= ");\n";
}
echo $sql;
?>

Research Names
02-08-2003, 12:13 AM
Fixed!