mysqli查询 - mysqli_sql_exception语法错误

时间:2012-03-13 10:04:06

标签: php mysql mysqli

大家好我从phpmyadmin执行的以下查询给了我没问题,但是使用mysqli扩展是一个噩梦,但只是在一个带变量的函数内。

$reference_field = 'attribute';
$value = $attribute; // it actual worths 'Size,S Small ,M Medium ,L Large ,XXL 2Xl XX-Large ,XXXL 3Xl XXX-Large ';
$original_value = md5($value);

 $SQL = " UPDATE jos_jf_content As j  SET j.value = '%s' ,  
j.original_value = '%s' WHERE j.reference_id  =%d  
AND j.reference_field  ='%s'  ";
echo $a=sprintf($SQL,$value,$original_value, $prodId,'attribute');
$Myconnector->real_query( $a   );

它给了我

mysqli_sql_exception: 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 3  

执行查询打印似乎一切正常并在单个php文件中单独执行以进行调试工作正常,只有在函数内部时才会给我带来麻烦

UPDATE jos_jf_content As j 
SET j.value = 'Size,S Small ,M Medium ,L Large ,XXL 2Xl XX-Large ' , 
j.original_value = 'f8508eb38ed3e26e6f3814a253b12c9a' , j.modified = 1331632841 
WHERE j.reference_id =1787 AND j.reference_field ='attribute'

1 个答案:

答案 0 :(得分:0)

我有根据的猜测是你有类似的东西:

$bar = 'Hello, World!';
$sql = sprintf("SELECT * FROM foo WHERE bar='%s'", $bar);

......并将其转化为:

function get_sql(){
    return sprintf("SELECT * FROM foo WHERE bar='%s'", $bar);
}
$bar = 'Hello, World!';
$sql = get_sql();

基本问题:

  1. 您尚未将PHP配置为显示通知。
  2. 您忽略了变量范围。
  3. 快速修复:

    ini_set('display_errors', TRUE);
    error_reporting(E_ALL | E_STRICT);
    
    function get_sql($bar){
        return sprintf("SELECT * FROM foo WHERE bar='%s'", $bar);
    }
    $bar = 'Hello, World!';
    $sql = get_sql($bar);