Connecting Tech Pros Worldwide Help | Site Map

fwrite problems ...

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 17th, 2005, 04:20 AM
Antoine Bloncourt
Guest
 
Posts: n/a
Default fwrite problems ...

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++;
}

  #2  
Old July 17th, 2005, 04:20 AM
Pedro Graca
Guest
 
Posts: n/a
Default Re: fwrite problems ...

Antoine Bloncourt wrote:[color=blue]
> 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.[/color]
....[color=blue]
> function get_table_structure($db, $table)//$db=nom de la base,$table=nom de la table
> {[/color]
(snipped)[color=blue]
> }
>
> 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";[/color]

echo shows its parameters in the browser.
[color=blue]
> $i++;
> }
> return (true);[/color]

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";

[color=blue]
> }
>
> /*-----------------------------------------------------------------------*/
> /* PROGRAMME PRINCIPAL[/color]
(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 :
  #3  
Old July 17th, 2005, 04:21 AM
Antoine Bloncourt
Guest
 
Posts: n/a
Default Re: fwrite problems ...

Pedro Graca <hexkid@hotpop.com> wrote in message news:<c3ndvh$29pji6$2@ID-203069.news.uni-berlin.de>...[color=blue]
> Antoine Bloncourt wrote:[color=green]
> > 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.[/color]
> ...[color=green]
> > function get_table_structure($db, $table)//$db=nom de la base,$table=nom de la table
> > {[/color]
> (snipped)[color=green]
> > }
> >
> > 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";[/color]
>
> echo shows its parameters in the browser.
>[color=green]
> > $i++;
> > }
> > return (true);[/color]
>
> 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";
>
>[color=green]
> > }
> >
> > /*-----------------------------------------------------------------------*/
> > /* PROGRAMME PRINCIPAL[/color]
> (snipped)[/color]

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 ...
  #4  
Old July 17th, 2005, 04:22 AM
Pedro Graca
Guest
 
Posts: n/a
Default Re: fwrite problems ...

Antoine Bloncourt wrote:[color=blue]
> 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 ...[/color]

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 :
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.