我有一个生成并运行mysql更新查询的函数。 当我在phpMyAdmin中运行生成的代码时,我没有麻烦,但是当我尝试从我的网站运行它时,我收到一个错误。 奇怪的是我使用相同的格式进行其他几个更新查询,并且它们正在完美地运行。
MySQL查询:
UPDATE `capc`.`bio_positions` SET `Pos_Name` = 'IT Specialist' WHERE `bio_positions`.`Pos_ID` = 63;UPDATE `capc`.`bio_positions` SET `Pos_Company` = 'CSG' WHERE `bio_positions`.`Pos_ID` = 63;
错误:
Could not get data: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'UPDATE `capc`.`bio_positions` SET `Pos_Company` = 'CSG' WHERE `bio_position' at line 1
用于生成MySQL查询的代码
function update($rank, $name, $company) {
if (!empty($rank) && $rank !== $this->Pos_Rank) {
$sql = "UPDATE `capc`.`bio_positions` SET `Pos_Rank` = " . $rank . " WHERE `bio_positions`.`Pos_ID` = " . $this->Pos_ID . ";";
}
if (!empty($name) && $name !== $this->Pos_Name) {
$sql .= "UPDATE `capc`.`bio_positions` SET `Pos_Name` = '" . $name . "' WHERE `bio_positions`.`Pos_ID` = " . $this->Pos_ID . ";";
}
if (!empty($company) && $company !== $this->Pos_Company) {
$sql .= "UPDATE `capc`.`bio_positions` SET `Pos_Company` = '" . $company . "' WHERE `bio_positions`.`Pos_ID` = " . $this->Pos_ID . ";";
}
echo "<br>" . $sql . "<br>";
if (!empty($sql)) {
$capc = new CAPC;
$capc->query($sql);
Bio_Position($this->Pos_ID);
}
}
从有效的答案更新更新功能
function update($rank, $name, $company) {
$capc = new CAPC;
$sql = "UPDATE `capc`.`bio_positions` SET ";
$go = 0;
if (!empty($rank) && $rank !== $this->Pos_Rank) {
$sql .= " `Pos_Rank` = " . $rank;
$go++;
}
if (!empty($name) && $name !== $this->Pos_Name) {
if($go > 0){
$comma = ",";
}
$sql .= $comma . " `Pos_Name` = '" . $name . "'";
$go++;
}
if (!empty($company) && $company !== $this->Pos_Company) {
if($go > 0){
$comma = ", ";
}
$sql .= $comma . " `Pos_Company` = '" . $company . "'";
$go++;
}
$sql .= " WHERE `bio_positions`.`Pos_ID` = " . $this->Pos_ID . ";";
if (!empty($sql) && $go > 0) {
//echo $sql . "<br>";
$capc = new CAPC;
$capc->query($sql);
}
}
答案 0 :(得分:2)
当我在phpMyAdmin中运行生成的代码时,我没有麻烦
更新
capc
。bio_positions
SETPos_Name
='IT专家'在哪里bio_positions
。Pos_ID
= 63;更新
capc
。bio_positions
设置Pos_Company
='CSG'WHERbio_positions
。Pos_ID
= 63;
虽然phpMyAdmin似乎一次执行多个查询(它实际上是在后端为你分割它们),但在你的代码中你需要单独执行它们或重写SQL来更新单个列中的多个列声明,即UPDATE capc.bio_positions SET Pos_Name = 'IT Specialist', Pos_Company = 'CSG' WHERE bio_positions.Pos_ID = 63
。
如果你正在使用mysqli,你也可以查看mysqli::multi_query。
答案 1 :(得分:1)
至少有一些MySQL API不允许在一个“批处理”中运行多个查询,但PHPMyAdmin允许它。
看到查询更新了同一组行,您可以将其重写为单个查询,这样可以加快执行速度,因为WHERE条件只需要评估一次;
UPDATE `capc`.`bio_positions`
SET `Pos_Name` = 'IT Specialist', `Pos_Company` = 'CSG'
WHERE `bio_positions`.`Pos_ID` = 63
答案 2 :(得分:1)
您的WHERE
具有相同的条件,那么为什么要将此分为三个查询?你可以这样做:
UPDATE `capc`.`bio_positions` SET `Pos_Rank` = " . $rank . ", `Pos_Name=` = " . $name . ", Pos_Company=" . $company . " WHERE `bio_positions`.`Pos_ID` = " . $this->Pos_ID;
编辑:(根据评论)
function update($rank, $name, $company) {
$new_rank = $this->Pos_Rank;
$new_name = $this->Pos_Name;
$new_company = $this->Pos_Company;
if (!empty($rank)) {
$new_rank = $rank;
}
if (!empty($name)) {
$new_name = $name;
}
if (!empty($company)) {
$new_company = $company;
}
$q = ...
}
答案 3 :(得分:0)
Try this
UPDATE `capc`.`bio_positions`
SET `Pos_Name` = 'IT Specialist'
WHERE `Pos_ID` = 63;