开发者

insert huge list of keywords to mysql with text doc

I have a huge list of keywords in a text doc that I want to dump into my keywords table in mysql开发者_开发技巧. Is there a way I can do this with some sort of php script?

My text doc contains keywords, 1 per line like so:

keyword1

keyword2

keyword3

keyword4

keyword5

etc

I want to be able to dump these keywords into my existing keywords table and not overwrite any possible existing keywords, just add in the new ones. Does anyone know how I could do this?

my table is called keywords, and the columns are keyword_id, keyword, time, total searches. Any help is appreciated, thanks.


You could also use LOAD DATA INFILE if you have the proper permissions in MySQL.


Make sure the keyword_id column has unique attribute set then read your txt line by line with fgets():

$handle = @fopen("huge.txt", "r");
if ($handle) {
    while (($buffer = fgets($handle)) !== false) {
        mysql_query("INSERT INTO keywords SET keyword_id = $buffer");
    }
    if (!feof($handle)) {
        echo "Error: unexpected fgets() fail\n";
    }
    fclose($handle);
}


you can use load data infile http://dev.mysql.com/doc/refman/5.1/en/load-data.html

load data infile 'keywords.dat' 
into table keywords
fields terminated by ',' optionally enclosed by '"'
lines terminated by '\r\n'
(
 keyword
);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜