如何以JSON格式检查PDO中的sql错误?

时间:2014-07-28 17:08:06

标签: php mysql json api pdo

我正在使用luracast restler来制作REST API。我正在尝试使用post方法更新用户。但我的sql没有执行,我不知道如何查找json格式的SQL错误。

我的API代码是

<?php
class User
{
public $dp;

function __construct()
{
    $this->dp = new DB_PDO_MySQL();
}
function post($request_data = NULL)
{

    $response = array();
    if(array_key_exists('user_id', $request_data)){
        $response = $this->dp->updateUser($request_data);
    }else{

        $response = $this->dp->signUp($request_data);
    }
    return $response;

 }

}
MySQL中的

和updateUser函数为

function updateUser($postData){

    $response = "";
    $data = array();
    if($this->checkToken($postData['token'])){
        $sql = $this->db->prepare('SELECT * FROM phpclassifieds_acc_users WHERE username =      :username');
        $sql->execute(array(':username' =>  $postData['username']));
        if($sql->rowCount()>0)
        {
           return $this->response(0,'','The Username ('.$postData[username].') is already in use');
        }

        $sql = $this->db->prepare('SELECT * FROM phpclassifieds_acc_users WHERE email = :email');
        $sql->execute(array(':email' =>  $postData['email']));
        if($sql->rowCount()>0)
        {
           return $this->response(0,'','The Email Address ('.$postData[email].') is already in use');

        }
        $sql = "UPDATE phpclassifieds_acc_users SET
                type = :type,
                username = :username,
                password = :password,
                name = :name,
                address =:address,
                address2 =:address2,
                address_city = :address_city,
                city =:city,
                zipcode =:zipcode,
                state =:state,
                email =:email,
                newsletter =:newsletter
                WHERE user_id = :user_id";



                try{



                $stmt =$this->db->prepare($sql);
                $stmt->bindParam(':type',$postData['type'],PDO::PARAM_STR);
                $stmt->bindParam(':username',$postData['username'],PDO::PARAM_STR);
                $stmt->bindParam(':password',$postData['password'],PDO::PARAM_STR);
                $stmt->bindParam(':name',$postData['name'],PDO::PARAM_STR);
                $stmt->bindParam(':address',$postData['address'],PDO::PARAM_STR);
                $stmt->bindParam(':address2',$postData['address2'],PDO::PARAM_STR);
                $stmt->bindParam(':address_city',$postData['address_city'],PDO::PARAM_STR);
                $stmt->bindParam(':city',$postData['city'],PDO::PARAM_STR);
                $stmt->bindParam(':zipcode',$postData['zipcode'],PDO::PARAM_STR);
                $stmt->bindParam(':state',$postData['state'],PDO::PARAM_STR);
                $stmt->bindParam(':email',$postData['email'],PDO::PARAM_STR);
                $stmt->bindParam(':user_id',$postData['user_id'],PDO::PARAM_INT);
                $var = $stmt->execute();
               } 
              catch(PDOException $e) {
                $err[] = var_dump($e->getMessage());

               echo json_encode($err);


                }

        if($var){

            $response = "Update Successfully done..";
            $sqlSelect = $this->db->prepare('SELECT * FROM phpclassifieds_acc_users WHERE user_id = :user_id');
            $sqlSelect->execute(array(':user_id'=>$postData['user_id']));
            $data =  $this->id2int($sqlSelect->fetch());
        }else{
            $response = "Update is unsuccessful ..";
        }
        return $this->response(1,$data,$response);
    }
  } 

问题是当我不使用try catch时输出是&#34;更新不成功....&#34;当我使用try catch我得到&#34;意想不到的令牌&#34;。

1 个答案:

答案 0 :(得分:0)

这行代码很奇怪

$err[] = var_dump($e->getMessage());
echo json_encode($err);

将var_dump编码为json是很奇怪的,你应该能够做到:

$err[] = $e->getMessage();
echo json_encode($err);

或者您可以从errorInfo()

获取错误
echo json_encode($this->db->errorInfo());

不要忘记绑定简报参数:

$stmt->bindParam(':newsletter',$postData['newsletter'],PDO::PARAM_STR);