By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,983 Members | 1,627 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,983 IT Pros & Developers. It's quick & easy.

fwrite problems ...

P: n/a
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_strucure" and "get_table_content"
functions.

Then I use fwrite() to write the result in a file.

I works for the get_table_structure (the result is written in the
file) but not for the get_table_content (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_structure($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_array($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_array($result))
{
$kname=$row['Key_name'];
if(($kname != "PRIMARY") && ($row['Non_unique'] == 0))
$kname="UNIQUE|$kname";
if(!isset($index[$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($columns, ", ") . ")";
elseif (substr($x,0,6) == "UNIQUE")
$schema_create .= " UNIQUE ".substr($x,7)." (" . implode($columns, ",
") . ")";
else
$schema_create .= " KEY $x (" . implode($columns, ", ") . ")";
}

$schema_create .= "\n)";
return (stripslashes($schema_create));
}

function get_table_content($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_fields($result);$j++)
$table_list .= mysql_field_name($result,$j).", ";

$table_list = substr($table_list,0,-2);
$table_list .= ")";

if(isset($GLOBALS["showcolumns"]))
$schema_insert = "INSERT INTO $table $table_list VALUES (";
else
$schema_insert = "INSERT INTO $table VALUES (";

for($j=0; $j<mysql_num_fields($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_insert).";\n";
$i++;
}
return (true);
}

/*-----------------------------------------------------------------------*/
/* PROGRAMME PRINCIPAL
*/
/*-----------------------------------------------------------------------*/

//connexion à la base
@set_time_limit(600);
@mysql_connect($host,$user,$pass)
or die("Impossible de se connecter - Problème sur le 'Hostname' ou sur
le 'User' ou sur le 'Password'");
@mysql_select_db("$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('dmY_Hi').".sql";

fopen("$nomFichier", "a+");
if (!$handle = fopen($nomFichier, 'a+')) {
echo "Impossible d'ouvrir le fichier ($nomFichier)";
exit;
}

$tables = mysql_list_tables($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_structure($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_content($db, $table), "\n".";\n\n");

if (isset($tb) && ($table==$tb))
exit;

$i++;
}
Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Antoine Bloncourt wrote:
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_strucure" and "get_table_content"
functions.

Then I use fwrite() to write the result in a file.

I works for the get_table_structure (the result is written in the
file) but not for the get_table_content (the result is displayed in
the browser).

I really don't understand what's wrong in this code. .... function get_table_structure($db, $table)//$db=nom de la base,$table=nom de la table
{ (snipped) }

function get_table_content($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_fields($result);$j++)
$table_list .= mysql_field_name($result,$j).", ";

$table_list = substr($table_list,0,-2);
$table_list .= ")";

if(isset($GLOBALS["showcolumns"]))
$schema_insert = "INSERT INTO $table $table_list VALUES (";
else
$schema_insert = "INSERT INTO $table VALUES (";

for($j=0; $j<mysql_num_fields($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_insert).";\n";
echo shows its parameters in the browser.
$i++;
}
return (true);
return returns to the calling function

I guess you want to remove the echo and replace the "return (true);"
statement with
return trim($schema_insert) . ";\n";

}

/*-----------------------------------------------------------------------*/
/* PROGRAMME PRINCIPAL

(snipped)

--
USENET would be a better place if everybody read: : mail address :
http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
http://www.expita.com/nomime.html : to 10K bytes :
Jul 17 '05 #2

P: n/a
Pedro Graca <he****@hotpop.com> wrote in message news:<c3*************@ID-203069.news.uni-berlin.de>...
Antoine Bloncourt wrote:
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_strucure" and "get_table_content"
functions.

Then I use fwrite() to write the result in a file.

I works for the get_table_structure (the result is written in the
file) but not for the get_table_content (the result is displayed in
the browser).

I really don't understand what's wrong in this code.

...
function get_table_structure($db, $table)//$db=nom de la base,$table=nom de la table
{

(snipped)
}

function get_table_content($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_fields($result);$j++)
$table_list .= mysql_field_name($result,$j).", ";

$table_list = substr($table_list,0,-2);
$table_list .= ")";

if(isset($GLOBALS["showcolumns"]))
$schema_insert = "INSERT INTO $table $table_list VALUES (";
else
$schema_insert = "INSERT INTO $table VALUES (";

for($j=0; $j<mysql_num_fields($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_insert).";\n";


echo shows its parameters in the browser.
$i++;
}
return (true);


return returns to the calling function

I guess you want to remove the echo and replace the "return (true);"
statement with
return trim($schema_insert) . ";\n";

}

/*-----------------------------------------------------------------------*/
/* PROGRAMME PRINCIPAL

(snipped)


Hi Pedro

Thank's for your answer but unfortunately, it does not work... I
really don't understand ...
It's still ok for the table structure (it is written in the .sql file)
but for the table content, it only write 1 line for per table in the
file ...
Jul 17 '05 #3

P: n/a
Antoine Bloncourt wrote:
It's still ok for the table structure (it is written in the .sql file)
but for the table content, it only write 1 line for per table in the
file ...


Maybe you need to fclose() the file before finishing the script ???

Turn on error_reporting for all errors in your script ... maybe
something shows up that will lead you in the right direction

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');

// rest of script
?>
--
USENET would be a better place if everybody read: : mail address :
http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
http://www.expita.com/nomime.html : to 10K bytes :
Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.