从JSON数组解码后,无法将数组传递给PDO预处理语句

时间:2013-12-04 16:20:31

标签: php function class pdo prepared-statement

请参阅底部的更新以获取最新信息!

我无法使用先编码然后解码的数据。 PDO准备好的语句无法识别数组中的变量。 $user已成功通过,但在execute时,所有变量似乎都消失了,变为NULL。出错的部分是什么?

代码:

class User {

    private $dbh;
    public function updateValue($user){     
        try{
            $sth = $this->dbh->prepare("UPDATE eq_question SET ". $user->field ."=? WHERE questID=?");

            $sth->execute(array($user->newvalue, $user->id));

            echo "|||SPACE|||Now its time to use it, they become... ";
            echo "user->newvalue-";
            var_dump( $user->newvalue);
            echo "user->id-";
            var_dump( $user->id);
            echo "EXECUTED RESULT---";

            var_dump( $sth->errorInfo());
            echo "SUCCESS!";
        }catch(PDOException $e){
        echo $e->getMessage();
    };
        return json_encode(1);  

    }
}
//Troubleshooters//////////Troubleshooters///////////////Troubleshooters////////////////Troubleshooters



$user = new stdClass;
$userParams = array('id' => 1, 'field' => 'questTitle', 'newvalue' => "Baaaaa");
$user = json_encode(array("user"=>$userParams));
echo " |||SPACE||| BEFORE DECODE-  ";
var_dump($user);
$user = json_decode($user);
echo " |||SPACE||| AFTER DECODE-  ";
var_dump($user);
$userN=new User(...); 
$dump=$userN->updateValue($user);
print($dump);
var_dump($dump);

返回结果:

|||SPACE||| BEFORE DECODE- string(58) "{"user":{"id":1,"field":"questTitle","newvalue":"Baaaaa"}}" 
|||SPACE||| AFTER DECODE- object(stdClass)#1 (1) { ["user"]=> object(stdClass)#2 (3) { ["id"]=> int(1) ["field"]=> string(10) "questTitle" ["newvalue"]=> string(6) "Baaaaa" } } 
|||SPACE||| INSIDE updateValue, the user is like this: object(stdClass)#1 (1) { ["user"]=> object(stdClass)#2 (3) { ["id"]=> int(1) ["field"]=> string(10) "questTitle" ["newvalue"]=> string(6) "Baaaaa" } } 
|||SPACE|||BEFORE START USING THEM...NULL NULL 
|||SPACE||| Now its time to use it, they become...user->newvalue-NULL user->id-NULL EXECUTED RESULT---array(3) { [0]=> string(5) "42000" [1]=> int(1064) [2]=> string(170) "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 '=NULL WHERE questID=NULL' at line 1" } SUCCESS! 
|||SPACE||| PRINT----- 1 
|||SPACE||| VAR_DUMP----- string(1) "1"

更新

我发现$ user数组DID已传入函数,但根本无法访问单个变量。任何人都可以告诉我可能是什么原因?

谢谢!

0 个答案:

没有答案