473,324 Members | 2,124 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

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++;
}
Jul 17 '05 #1
3 2425
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: leonecla | last post by:
Hi everybody, I'm facing a very very strange problem with a very very simple C program... My goal should be to write to a binary file some numbers (integers), each one represented as a sequence...
3
by: sumit1680 | last post by:
Hi everyone, I am using the below listed code The code is #include<stdio.h> #include<stdlib.h> #include<string.h>
4
by: janssenssimon | last post by:
//de structure om de highscores in op de slagen typedef struct score{ char *naam; int veld; int score; struct score *volg; }HIGH; void toonhighscores(void)
1
by: Skeets | last post by:
i'm having some php problems. i have the following code on my main page: ------------ $fp = fsockopen("127.0.0.1", 49152, $errno, $errrstr, 30); if (!$fp) { echo "socket error: $errstr...
2
by: elisa | last post by:
Dear all, I have problems in writeing and reading a block of data (long array) with fread and fwrite. If I write and read an integer array, everything looks fine, but when I try long array, sth...
3
by: Vivienne | last post by:
I am using VS 2005. In a project when I was trying to write some unsigned char into a file, I used fwrite() and fputc(). But I found whenever I tried to write 0x0a, fwrite() function automaticly...
1
by: DreAAM | last post by:
Hi, I am having problems with fwrite for large arrays, and would appriciate any help. In a small test program, this doesn't work: fwrite(temp, 4303355904, 1, OUT) The resulting array is only...
19
by: pal | last post by:
Hi all, Could you help me how to use the fwrite( ) in C ++
4
by: Highlander2nd | last post by:
Hello there. I'm Andrew Lucas, I'm a programmer for Half-Life. I've been working on stencil shadows lately, and I've been having problems saving mesh data for my models. When I store mesh data, I...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.