tags:

views:

38

answers:

3

hello, i have written a php script to insert the contents of a text file into mysql database and the content of my file a.txt is

\n is used for a new line

i want it to be loaded into the database including the special character \n

as it is... But it is loading only " is used for a new line" kindly help me out... thanx

<?php
$filename = "a.txt";
$f = fopen($filename, 'r');
$total_contents = fread($f, filesize($filename));
print $total_contents;
mysql_connect("localhost", "datab_user", "password") or die(mysql_error());
mysql_select_db("datab_name") or die(mysql_error());
mysql_query("INSERT INTO table_name
(id,content) VALUES( 333,'$total_contents' ) ")
or die(mysql_error());
?>
A: 

You have to escape it. If you want a newline in Mysql, send \n to the server, if you want a \n, send \\n

joni
A: 

If you want the \n to appear inside the DB as is (ie. as \ followed by n and not as a newline), then you have to escape it, so you have to send \\n is used for a new line to the DB.

BTW You could also use file_get_contents if you just want to read the full file contents into a string.

wimvds
A: 

Try taking a look at the PHP manual for mysql_real_escape_string to escape input into a mysql databse.

Koorb
thanx... it worked...