fwrite problems ...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Antoine Bloncourt

    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_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++;
    }
  • Pedro Graca

    #2
    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_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.[/color]
    ....[color=blue]
    > function get_table_struc ture($db, $table)//$db=nom de la base,$table=nom de la table
    > {[/color]
    (snipped)[color=blue]
    > }
    >
    > 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";[/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_in sert) . ";\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 :

    Comment

    • Antoine Bloncourt

      #3
      Re: fwrite problems ...

      Pedro Graca <hexkid@hotpop. com> wrote in message news:<c3ndvh$29 pji6$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_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.[/color]
      > ...[color=green]
      > > function get_table_struc ture($db, $table)//$db=nom de la base,$table=nom de la table
      > > {[/color]
      > (snipped)[color=green]
      > > }
      > >
      > > 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";[/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_in sert) . ";\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 ...

      Comment

      • Pedro Graca

        #4
        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('displa y_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 :

        Comment

        Working...