Php 如何返回mysql表中不存在的逗号分隔值?
我正在尝试创建一个tags表来接受来自用户的新标签 我想排除像或那样插入到标记表中的常规单词 我的想法是创建一个通用单词表,并在插入新标签时排除它们 这是我如何使用php的:Php 如何返回mysql表中不存在的逗号分隔值?,php,mysql,Php,Mysql,我正在尝试创建一个tags表来接受来自用户的新标签 我想排除像或那样插入到标记表中的常规单词 我的想法是创建一个通用单词表,并在插入新标签时排除它们 这是我如何使用php的: //get the general words from database and convert them into ary_general //execlude the general words from the new words and store the rest into ary_tags //inser
//get the general words from database and convert them into ary_general
//execlude the general words from the new words and store the rest into ary_tags
//insert the ary_tags words into tags table if doesn't exist.
但在一份声明中,我希望尽我所能:
例如:
来源:do,you,think,that,programming,is,cool
tbl_general_words
do
you
that
is
结果:
思考、编程、酷一句话的方法可以奏效
<?php
//needs to be configured appropriately
$conn = new PDO($dsn, $user, $password);
// fetch associative arrays, I normally use objects myself though
$conn->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
// split the string in question into words
$wordsToTag = explode(" ", $phraseToTag);
// create a holder for the common words
$commonWords = array();
// prepare and execute the statement
$stmt = $conn->prepare("SELECT word FROM tbl_general_words");
$stmt->execute();
// add the words to the array
while($row = $stmt->fetch())
{
$commonWords[] = $row['word'];
}
// get the words that are not common
$wordsToTag = array_diff($commonWords, $wordsToTag);
// build your SQL string
$sql = "INSERT INTO tags (tag) VALUES ";
// fore each word, we'll need a ? to hold the paramater
foreach($wordsToTag as $wordToTag)
{
$sql .= "(?), ";
}
// remove the trailing space and trailing comma
// this could be achieved with an array and join instead if you prefer
$sql = rtrim(trim($sql), ",");
// add the words
$stmt = $conn->prepare($sql);
$stmt->execute($wordsToTag);
?>
但是,我建议将其作为一种迭代的方法,因为它更干净
<?php
// the code is the same until the adding part
$conn = new PDO($dsn, $user, $password);
$conn->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
$wordsToTag = explode(" ", $phraseToTag);
$commonWords = array();
$stmt = $conn->prepare("SELECT word FROM tbl_general_words");
$stmt->execute();
while($row = $stmt->fetch())
{
$commonWords[] = $row['word'];
}
$wordsToTag = array_diff($commonWords, $wordsToTag);
// prepare the statement
$stmt = $conn->prepare("INSERT INTO tags SET tag = ?");
foreach($wordsToTag as $wordToTag)
{
// change the param and execute, because prepared statements are designed for this
$stmt->bindParam(1, $wordToTag);
$stmt->execute();
}
?>
创建临时表临时标签word VARCHAR100不为空;
在临时_标记中插入值“do”、“you”、“think”、“that”、“programming”、“is”、“cool”;
-mysql_queryINSERT为临时_标记值,'内爆',',$arr_标记';
插入标记tagColumn、someIntColumn、someStrColumn
选择temporary_tags.word,5,“字符串值”
从临时标签
左键连接tbl_general_单词在tbl_general_单词上。单词=临时标记。单词
其中tbl_general_words.word为空;
删除临时表临时_标记;如果每个单词都在自己的行中,而不是存储为逗号分隔的列表,则可以很容易地完成此操作。您是指源字符串还是常规表?请为tags table显示表架构为什么只有一条语句?为什么不提取要剥离的单词,如果句子中的单个单词不在该数组中,则将其添加到标记中?预处理语句的开销非常小,特别是在不使用大型数据集的情况下。如果您使用的是大型数据集,则可以创建一个预先准备好的语句,一次插入不在该数组中的所有单词。我认为这对于保存数据库请求负载来说是一个更好的主意,因为每次尝试检查标记时,我都必须选择所有常规单词。