简体   繁体   中英

php script to load contents of a file into mysql database

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());
?>

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

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.

尝试查看PHP手册中的mysql_real_escape_string,以将输入转义到mysql数据库中。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM