PHP将字符串分解成碎片然后插入到mysql中

发布于 2024-12-01 23:38:01 字数 446 浏览 2 评论 0原文

如何将字符串分解成碎片然后插入mysql?

我想按句子插入每个单词,然后插入到mysql数据库中。但我不确定$str的长度是多少,可能是4个字,也可能是10个字。

那么如何制作一个foreach,将每个单词插入数据库(每个单词一行),谢谢。

$str = "This is a test";
$piece = explode(' ',$str);
$num=0;
for(){
...
mysql_query("INSERT INTO test (words) SELECT '".addslashes($piece[$num])."' FROM dual WHERE not exists (SELECT words FROM test WHERE test.words = '".addslashes($piece[$num])."')");
$num++; 
}

How to explode string into pieces then insert into mysql?

I want to insert every word by the sentence, then insert into mysql database. But I am not sure what is the length of the $str, it may be 4 words, also can be 10 words.

So how to make a foreach, insert each word into a database (each word in one line), Thanks.

$str = "This is a test";
$piece = explode(' ',$str);
$num=0;
for(){
...
mysql_query("INSERT INTO test (words) SELECT '".addslashes($piece[$num])."' FROM dual WHERE not exists (SELECT words FROM test WHERE test.words = '".addslashes($piece[$num])."')");
$num++; 
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

困倦 2024-12-08 23:38:01

非常简单,也不要使用 addslashes(),我会使用 mysql_escape_string() 来处理与 mysql 相关的所有内容。

$str = "This is a test"; 
$pieces = explode(' ', $str);
foreach($pieces as $piece)
    mysql_query('insert into test (words) values (\'' . $piece . '\');');

(当然你可以再次添加更多条件,例如确保单词唯一等)

foreach() 相当容易使用:

foreach($array as $value)

or

foreach($array as $key => $value)

where $key and $value 每次迭代都会更新。

Pretty easy, also don't use addslashes(), I'd use mysql_escape_string() for everything mysql related.

$str = "This is a test"; 
$pieces = explode(' ', $str);
foreach($pieces as $piece)
    mysql_query('insert into test (words) values (\'' . $piece . '\');');

(Of course you can add more conditions again, e.g. ensure words are unique, etc.)

foreach() is rather easy to use:

foreach($array as $value)

or

foreach($array as $key => $value)

where $key and $value are updated each iteration.

陈甜 2024-12-08 23:38:01
$str = "This is a test";
$piece = explode(' ',$str);
foreach($piece as $substr){
    mysql_query("INSERT INTO test (words) SELECT '".mysql_real_escape_string($substr)."' FROM dual WHERE not exists (SELECT words FROM test WHERE test.words = '".mysql_real_escape_string($substr)."')");
}
$str = "This is a test";
$piece = explode(' ',$str);
foreach($piece as $substr){
    mysql_query("INSERT INTO test (words) SELECT '".mysql_real_escape_string($substr)."' FROM dual WHERE not exists (SELECT words FROM test WHERE test.words = '".mysql_real_escape_string($substr)."')");
}
柠檬心 2024-12-08 23:38:01
$str = "This is a test";

// make sure there are only letters and spaces in your text (prevents
// something like "demo," in your database
$str = preg_replace("/[^a-zA-Z0-9\ ]/", " ",$str);

// explode it
$words = explode(' ',$str);

// make sure we have unique words
$words = array_unique($words);

foreach ($words as $word) {
    mysql_query("INSERT INTO test (word) values ('".$word."')";
}

$str = "This is a test";

// make sure there are only letters and spaces in your text (prevents
// something like "demo," in your database
$str = preg_replace("/[^a-zA-Z0-9\ ]/", " ",$str);

// explode it
$words = explode(' ',$str);

// make sure we have unique words
$words = array_unique($words);

foreach ($words as $word) {
    mysql_query("INSERT INTO test (word) values ('".$word."')";
}
望喜 2024-12-08 23:38:01

同样,如果您想减少运行的查询数量(这反过来会减少脚本运行所需的时间):

<?php
$str="This is a test";
$words=explode(' ',mysql_escape_string($str));
$query="insert into test (words) values ('".implode("'),('",array_unique($words))."')";
$result=mysql_query($query);
?>

它本质上用 '),(' 替换所有空格,然后将整个内容包装到一个查询

Similarly, if you want to decrease the number of queries you run (which, in turn, will decrease the amount of time your script takes to run):

<?php
$str="This is a test";
$words=explode(' ',mysql_escape_string($str));
$query="insert into test (words) values ('".implode("'),('",array_unique($words))."')";
$result=mysql_query($query);
?>

It essentially replaces all spaces with '),(' and then wraps the whole thing into a query

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文