Hello everybody
Sorry to bother you but I have a problem writing datas into a file ...
I want to make a backup of my MySQL database and put the result into a
..sql file.
To do this, I use the "get_table_stru cure" and "get_table_cont ent"
functions.
Then I use fwrite() to write the result in a file.
I works for the get_table_struc ture (the result is written in the
file) but not for the get_table_conte nt (the result is displayed in
the browser).
I really don't understand what's wrong in this code.
Thank you for your help
Antoine
function get_table_struc ture($db, $table)//$db=nom de la
base,$table=nom de la table
{
global $drop;
$schema_create = "";
if(!empty($drop ))
$schema_create .= "DROP TABLE IF EXISTS $table;\n";
$schema_create .= "CREATE TABLE $table (\n";
$result = mysql_db_query( $db, "SHOW FIELDS FROM $table") or
mysql_die();
while($row = mysql_fetch_arr ay($result))
{
$schema_create .= " $row[Field] $row[Type]";
if(isset($row["Default"]) && (!empty($row["Default"]) ||
$row["Default"] == "0"))
$schema_create .= " DEFAULT '$row[Default]'";
if($row["Null"] != "YES")
$schema_create .= " NOT NULL";
if($row["Extra"] != "")
$schema_create .= " $row[Extra]";
$schema_create .= ",\n";
}
$schema_create = ereg_replace(", \n$", "", $schema_create) ;
$result = mysql_db_query( $db, "SHOW KEYS FROM $table") or
mysql_die();
while($row = mysql_fetch_arr ay($result))
{
$kname=$row['Key_name'];
if(($kname != "PRIMARY") && ($row['Non_unique'] == 0))
$kname="UNIQUE| $kname";
if(!isset($inde x[$kname]))
$index[$kname] = array();
$index[$kname][] = $row['Column_name'];
}
while(list($x, $columns) = @each($index))
{
$schema_create .= ",\n";
if($x == "PRIMARY")
$schema_create .= " PRIMARY KEY (" . implode($column s, ", ") . ")";
elseif (substr($x,0,6) == "UNIQUE")
$schema_create .= " UNIQUE ".substr($x,7). " (" . implode($column s, ",
") . ")";
else
$schema_create .= " KEY $x (" . implode($column s, ", ") . ")";
}
$schema_create .= "\n)";
return (stripslashes($ schema_create)) ;
}
function get_table_conte nt($db, $table){//$db=nom de la
base,$table=nom de la table
$result = mysql_db_query( $db, "SELECT * FROM $table") or mysql_die();
$i = 0;
while($row = mysql_fetch_row ($result))
{
$table_list = "(";
for($j=0; $j<mysql_num_fi elds($result);$ j++)
$table_list .= mysql_field_nam e($result,$j)." , ";
$table_list = substr($table_l ist,0,-2);
$table_list .= ")";
if(isset($GLOBA LS["showcolumn s"]))
$schema_insert = "INSERT INTO $table $table_list VALUES (";
else
$schema_insert = "INSERT INTO $table VALUES (";
for($j=0; $j<mysql_num_fi elds($result);$ j++)
{
if(!isset($row[$j]))
$schema_insert .= " NULL,";
elseif($row[$j] != "")
$schema_insert .= " '".addslashes($ row[$j])."',";
else
$schema_insert .= " '',";
}
$schema_insert = ereg_replace(", $", "", $schema_insert) ;
$schema_insert .= ")";
echo trim($schema_in sert).";\n";
$i++;
}
return (true);
}
/*-----------------------------------------------------------------------*/
/* PROGRAMME PRINCIPAL
*/
/*-----------------------------------------------------------------------*/
//connexion à la base
@set_time_limit (600);
@mysql_connect( $host,$user,$pa ss)
or die("Impossible de se connecter - Problème sur le 'Hostname' ou sur
le 'User' ou sur le 'Password'");
@mysql_select_d b("$db")
or die("Impossible de se connecter à la base ou nom de base inconnu");
//creation du fichier de sauvegarde (enregistrement en local)
$nomFichier = "bdd_".date('dm Y_Hi').".sql";
fopen("$nomFich ier", "a+");
if (!$handle = fopen($nomFichi er, 'a+')) {
echo "Impossible d'ouvrir le fichier ($nomFichier)";
exit;
}
$tables = mysql_list_tabl es($db);//Liste les tables d'une base de
données.
$num_tables = @mysql_numrows( $tables);//Retourne le nombre de lignes
d'un résultat
$i = 0;
while($i < $num_tables)
{
$table = mysql_tablename ($tables, $i);//Lit le nom de la table qui
contient le champs spécifié
fwrite($handle, "\n");
fwrite($handle, "#
--------------------------------------------------------\n");
fwrite($handle, "# Structure de la table \"$table\"\n ");
fwrite($handle, "#
--------------------------------------------------------\n");
fwrite($handle, "#\n\n");
fwrite($handle, get_table_struc ture($db, $table, "\n").";\n" );
fwrite($handle, "#
--------------------------------------------------------\n");
fwrite($handle, "# Contenu de la table \"$table\"\n ");
fwrite($handle, "#
--------------------------------------------------------\n");
fwrite($handle, "#\n\n");
fwrite($handle, get_table_conte nt($db, $table), "\n".";\n\n ");
if (isset($tb) && ($table==$tb))
exit;
$i++;
}
Sorry to bother you but I have a problem writing datas into a file ...
I want to make a backup of my MySQL database and put the result into a
..sql file.
To do this, I use the "get_table_stru cure" and "get_table_cont ent"
functions.
Then I use fwrite() to write the result in a file.
I works for the get_table_struc ture (the result is written in the
file) but not for the get_table_conte nt (the result is displayed in
the browser).
I really don't understand what's wrong in this code.
Thank you for your help
Antoine
function get_table_struc ture($db, $table)//$db=nom de la
base,$table=nom de la table
{
global $drop;
$schema_create = "";
if(!empty($drop ))
$schema_create .= "DROP TABLE IF EXISTS $table;\n";
$schema_create .= "CREATE TABLE $table (\n";
$result = mysql_db_query( $db, "SHOW FIELDS FROM $table") or
mysql_die();
while($row = mysql_fetch_arr ay($result))
{
$schema_create .= " $row[Field] $row[Type]";
if(isset($row["Default"]) && (!empty($row["Default"]) ||
$row["Default"] == "0"))
$schema_create .= " DEFAULT '$row[Default]'";
if($row["Null"] != "YES")
$schema_create .= " NOT NULL";
if($row["Extra"] != "")
$schema_create .= " $row[Extra]";
$schema_create .= ",\n";
}
$schema_create = ereg_replace(", \n$", "", $schema_create) ;
$result = mysql_db_query( $db, "SHOW KEYS FROM $table") or
mysql_die();
while($row = mysql_fetch_arr ay($result))
{
$kname=$row['Key_name'];
if(($kname != "PRIMARY") && ($row['Non_unique'] == 0))
$kname="UNIQUE| $kname";
if(!isset($inde x[$kname]))
$index[$kname] = array();
$index[$kname][] = $row['Column_name'];
}
while(list($x, $columns) = @each($index))
{
$schema_create .= ",\n";
if($x == "PRIMARY")
$schema_create .= " PRIMARY KEY (" . implode($column s, ", ") . ")";
elseif (substr($x,0,6) == "UNIQUE")
$schema_create .= " UNIQUE ".substr($x,7). " (" . implode($column s, ",
") . ")";
else
$schema_create .= " KEY $x (" . implode($column s, ", ") . ")";
}
$schema_create .= "\n)";
return (stripslashes($ schema_create)) ;
}
function get_table_conte nt($db, $table){//$db=nom de la
base,$table=nom de la table
$result = mysql_db_query( $db, "SELECT * FROM $table") or mysql_die();
$i = 0;
while($row = mysql_fetch_row ($result))
{
$table_list = "(";
for($j=0; $j<mysql_num_fi elds($result);$ j++)
$table_list .= mysql_field_nam e($result,$j)." , ";
$table_list = substr($table_l ist,0,-2);
$table_list .= ")";
if(isset($GLOBA LS["showcolumn s"]))
$schema_insert = "INSERT INTO $table $table_list VALUES (";
else
$schema_insert = "INSERT INTO $table VALUES (";
for($j=0; $j<mysql_num_fi elds($result);$ j++)
{
if(!isset($row[$j]))
$schema_insert .= " NULL,";
elseif($row[$j] != "")
$schema_insert .= " '".addslashes($ row[$j])."',";
else
$schema_insert .= " '',";
}
$schema_insert = ereg_replace(", $", "", $schema_insert) ;
$schema_insert .= ")";
echo trim($schema_in sert).";\n";
$i++;
}
return (true);
}
/*-----------------------------------------------------------------------*/
/* PROGRAMME PRINCIPAL
*/
/*-----------------------------------------------------------------------*/
//connexion à la base
@set_time_limit (600);
@mysql_connect( $host,$user,$pa ss)
or die("Impossible de se connecter - Problème sur le 'Hostname' ou sur
le 'User' ou sur le 'Password'");
@mysql_select_d b("$db")
or die("Impossible de se connecter à la base ou nom de base inconnu");
//creation du fichier de sauvegarde (enregistrement en local)
$nomFichier = "bdd_".date('dm Y_Hi').".sql";
fopen("$nomFich ier", "a+");
if (!$handle = fopen($nomFichi er, 'a+')) {
echo "Impossible d'ouvrir le fichier ($nomFichier)";
exit;
}
$tables = mysql_list_tabl es($db);//Liste les tables d'une base de
données.
$num_tables = @mysql_numrows( $tables);//Retourne le nombre de lignes
d'un résultat
$i = 0;
while($i < $num_tables)
{
$table = mysql_tablename ($tables, $i);//Lit le nom de la table qui
contient le champs spécifié
fwrite($handle, "\n");
fwrite($handle, "#
--------------------------------------------------------\n");
fwrite($handle, "# Structure de la table \"$table\"\n ");
fwrite($handle, "#
--------------------------------------------------------\n");
fwrite($handle, "#\n\n");
fwrite($handle, get_table_struc ture($db, $table, "\n").";\n" );
fwrite($handle, "#
--------------------------------------------------------\n");
fwrite($handle, "# Contenu de la table \"$table\"\n ");
fwrite($handle, "#
--------------------------------------------------------\n");
fwrite($handle, "#\n\n");
fwrite($handle, get_table_conte nt($db, $table), "\n".";\n\n ");
if (isset($tb) && ($table==$tb))
exit;
$i++;
}
Comment