请检查我的代码,我得到以下输出:
in wood that 1
in wood that 2
in wood that 3
in wood that 4
从下面显示的代码中,我希望这些四行在我的MySQL数据库中存储。
$string = "imperfection in wood that 1 can appear during the wood drying process.imperfection in wood that 2 can appear during the wood drying process.imperfection in wood that 3 can appear during the wood drying process.imperfection in wood that 4 can appear during the wood drying process";
preg_match_all('/(imperfection)(.*?)(can)/', $string, $matches);
foreach($matches[2] as $value)
echo $value.'<br />';
$sql="INSERT INTO string_check(st_name)VALUES($value)";
$result=mysql_query($sql);
谢谢&amp;亲切的问候,
答案 0 :(得分:0)
迭代匹配,逃避它们(以防万一)并将它们包装在引号和括号中;然后用逗号连接所有这些字符串,并将该字符串用作VALUES表达式:
$foreach($matches[2] as $match) {
$values[] = '("' . mysql_real_escape_string($match) . '")';
}
$value_statement = implode(', ', $values);
$sql="INSERT INTO string_check (st_name) VALUES $value_statement";
答案 1 :(得分:0)
您打算将这四行存储到不同的行中吗?如果是这种情况,那么试试这个
foreach($matches[2] as $value) {
$sql="INSERT INTO string_check(st_name)VALUES($value)";
$result=mysql_query($sql);
}
如果要将其存储在一行中,请使用以下语法
$values = '';
foreach($matches[2] as $value) {
$values .= $value;
}
$sql="INSERT INTO string_check(st_name)VALUES($values)";
$result=mysql_query($sql);