- 论坛徽章:
- 0
|
PHP input txt to mysql求助
- <?php
- // -- 数据库主机名
- $db_host = "localhost";
- // -- 数据库名称
- $db_name = "test";
- // -- 数据库用户名
- $db_user = "test";
- // -- 数据库用户口令
- $db_pass = "test";
- // -- 需要插入的数据表名称
- $table_name = "test";
- // -- 需要读取数据的文本文件
- $file_name = "./input .txt";
- // -- SQL语句模板
- $sql_tpl = "INSERT INTO $table_name (name, info) VALUES ('%s', '%s')";
- $file_text = file($file_name) or die("Could not read the file $file_name");
- for ($i=0; $i<count($file_text); $i++) {
- $tmp_text = explode(" ", $file_text[$i]);
- if (count($tmp_text) >; 2) $tmp_text[1] = $tmp_text[2];
- $sql_query[] = sprintf($sql_tpl, $tmp_text[0], $tmp_text[1]);
- }
- unset($file_text);
- $db_id = @mysql_connect($db_host, $db_user, $db_pass) or die("could not connect to DB host");
- @mysql_select_db($db_name, $db_id) or die("could not select the database $db_name");
- for ($i=0; $i<count($sql_query); $i++) {
- if (@mysql_query($sql_query[$i], $db_id)) echo "Line " . ($i+1) . " : successful!";
- else echo "Line " . ($i+1) . " : False!";
- }
- @mysql_close($db_id);
- ?>;
复制代码 |
|