Hello php gurus!
i need help with a little script, i have to import a tab delimited files into a mysql db
file.tab
"user1"TAB"pass1"TAB"miscdata1_1"TAB"miscdata1_2"TAB"miscdata1_3"TAB"miscdata1_4"TAB
"user2"TAB"pass2"TAB"miscdata2_1"TAB"miscdata2_2"TAB"miscdata2_3"TAB"miscdata2_4"TAB
Here is what i have programmed with the help of google
<?
include('config.php');
$conn = mysql_connect($dbserver, $dbuser, $dbpass);
if (!$conn) {
echo "Impossible de se connecter la base de donns : " . mysql_error();
exit;
}
if (!mysql_select_db($dbname)) {
echo "Impossible d'accer la base de donns : " . mysql_error();
exit;
}
$fcontents = file('./file.tab');
for($i=0; $i<sizeof($fcontents); $i++) {
$line = trim($fcontents[$i]);
$arr = explode("\t", $line);
#if your data is comma separated
# instead of tab separated,
# change the '\t' above to ';'
$sql = "insert into accounts values ('". implode("','", $arr) ."')";
mysql_query($sql);
echo $sql ."<br>\n";
if(mysql_error()) {
echo mysql_error() ."<br>\n";
}
}
?>
This code will import the data in my database, something is annoying, all the db fields are filled with values with ""
This isnt a big problem, i can deal with it.
The second part of my script needs to be able to export the mysql database into a tab delimited file (same kind of file than file.tab) So i can add new entry in the database, export the content of the db to this file.
This is where i am jammed, i dont know how to do that, i dont know how to export the content of a db into a file to get the same result as the original file.tab
Any of the php masters here can help me? thanks in advance
fred
Sorry for the terrible english.