简体   繁体   English

将txt文件导入Mysql

[英]Import txt file to Mysql

I have a problem and would like to know if anybody can help me.. 我有问题,想知道是否有人可以帮助我。

I have a text file containing data separated by "," and I have a table where I need to import them into. 我有一个文本文件,其中包含用“,”分隔的数据,并且我有一个表需要将其导入。 Here is what I have so far. 这是我到目前为止所拥有的。

This is my contact.php file 这是我的contact.php文件

<?php

$dbH = mysql_connect('localhost', 'user', 'pass') or die('Could not connect to MySQL server.<br>' . mysql_error()); 


mysql_select_db('test') or die('Could not select database.<br>' . mysql_error(); 


$CSVFile = 'import.txt';


mysql_query('LOAD DATA LOCAL INFILE "import.txt" INTO TABLE wp_contact_employees FIELDS TERMINATED BY "," LINES TERMINATED BY "\r\n";') or die('Error loading data file.<br>' . mysql_error());


mysql_close($dbH);
?> 

this is my import.txt file.. 这是我的import.txt文件。

kagn@test.com, Kralle G, 99998888 \\n
many@test.com, Martin N, 99876543 \\n

And in my "wp_contact_employees" I want the above to be inserted in (mail, name, tel) column. 在“ wp_contact_employees”中,我希望将以上内容插入(邮件,姓名,电话)列中。

Im new to all this php, and im stuck now :( 我是所有这些php的新手,并且现在卡住了:(

LOAD DATA LOCAL INFILE requires that the file pathname you specify be visible to the mysqld server process. LOAD DATA LOCAL INFILE要求您指定的文件路径名对mysqld服务器进程可见。 Your server is on localhost, so that means MySQL is running on the same machine as your php. 您的服务器位于localhost上,这意味着MySQL与php在同一台计算机上运行。 That's a good start. 这是一个好的开始。

Now you need to figure out the full pathname for your import.txt file. 现在,您需要找出import.txt文件的完整路径名。 This depends on how your server is set up, but it may be something like 这取决于您的服务器设置方式,但可能类似于

 /home/user3433414/public_ftp/data/import.txt

Once you have that working, you may need to change the protection of your .txt file so it is readable by the world. 工作完成后,您可能需要更改对.txt文件的保护,以便world.都可以阅读world.

It's unfortunately impossible to tell you in detail what to do without knowing the ins and outs of your hosting provider's server setup. 不幸的是,如果不知道托管服务提供商的服务器设置的详细信息,就无法详细告诉您该怎么做。 Perhaps you should ask for help from their support team. 也许您应该向他们的支持团队寻求帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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