簡體   English   中英

PHP如何將數組數據保存到數據庫

[英]php how to save array data to database

請檢查我的代碼,我得到以下輸出:

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);

謝謝與親切的問候,

遍歷匹配項,轉義它們(以防萬一),並用引號和括號括起來; 然后用逗號將它們全部連接起來,並使用該字符串作為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";

您是否要將這四行存儲在不同的行中? 如果是這種情況,請嘗試

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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM