我已经切换到PDO时遇到麻烦构建和执行一个SQL查询,将插入多行只有一个执行。
内容$data
json_decode后:
Array (
[action] => load
[app] => CA
[street_type] => AVE
[place_type] => --
[state] => AL
)
码:
$data = json_decode(file_get_contents("php://input"));
$query = "REPLACE INTO tblsettings(setApp, setIP, setKey, setValue)VALUES";
$qPart = array_fill(0, count($data), "(?, ?, ?, ?)");
$query .= implode(",", $qPart);
$stmt = $db->prepare($query);
foreach($data as $key => $val){
$query = "REPLACE INTO tblsettings(setApp, setIP, setKey, setValue)VALUES";
$qPart = array_fill(0, count($data), "(?, ?, ?, ?)");
$query .= implode(",", $qPart);
$stmt = $db->prepare($query);
$i = 1;
if(!is_array($val)){
$stmt->bindParam($i++, $data->app);
$stmt->bindParam($i++, gethostbyname(trim(gethostname())));
$stmt->bindParam($i++, $key);
$stmt->bindParam($i++, $val);
}
if ($stmt->execute()){
echo "Success";
}else{
echo $stmt->errorCode();
}
}