SQL PHP语法错误优化

时间:2011-02-19 04:56:37

标签: php arrays syntax query-optimization

我在此代码中出错。有谁看到我做错了什么。这是我得到的错误:

Died 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 ')' at line 1

这是代码:

   $depSQL = mysql_query("SELECT department_array FROM busDetails WHERE bus_id='".$bus_id."' LIMIT 1"); 
   while($row=mysql_fetch_array($depSQL)) 
   { 
     $depARRAY = $row["department_array"]; 
   }
    $stSQL = mysql_query("SELECT department_id, staff_array FROM institute_departments WHERE department_id IN ($depARRAY)") or die ("Died ".mysql_error());
   while($get=mysql_fetch_array($stSQL)) { 
   $stARRAY = $get["staff_array"];
     $id =  $get["department_id"];
   $explodedST = explode(",", $stARRAY);
  foreach ($explodedST as $key2 => $value2) {
        if ($value2 == $bus_id) {
            unset($explodedST[$key2]);
        }
}
$newST = implode(",", $explodedST);
echo $id . " " . $newST . "<BR/>" ;
$sql = mysql_query("UPDATE institute_departments SET staff_array ='$newST' WHERE     department_id='$id'");
 }

2 个答案:

答案 0 :(得分:2)

回应您的查询,您可能会看到它。

示例:

echo $stSQL;

你的逗号太多了。照看9:“7,8,9,”

答案 1 :(得分:1)

$strArray = explode(',' , trim($depARRAY));

$count = count($strArray);
if(empty($strArray[$count-1]))
    unset($strArray[$count-1]);

$depARRAY = implode(',' , $strArray);

插入值时,您必须注意extra comma