首頁 > 後端開發 > php教程 > mysql - php中使用PDO查詢資料庫出錯

mysql - php中使用PDO查詢資料庫出錯

WBOY
發布: 2016-09-19 09:16:34
原創
1206 人瀏覽過

查詢的程式碼是:

<code>$email=htmlspecialchars($_POST["email"]);
if($sql->getData('SELECT * FROM user WHERE email='.$email)==NULL){
</code>
登入後複製
登入後複製

getData的程式碼是:

<code>public function getData($sql,$type=1){
    $data=Array();
    $db=$this->db();

    $result=$db->query($sql);
    $sth = $db->prepare($sql);
    $sth->execute();
    if(is_bool($result))
        return $result;
    if($type==1)
        while($a = $sth->fetch(PDO::FETCH_ASSOC))
            $data[]=$a;
    elseif($type==2)
        while($a = $sth->fetch(PDO::FETCH_BOTH))
            $data[]=$a;

    if($data)
        return $data;
    else
        return NULL;
}
</code>
登入後複製
登入後複製

錯誤提示是:

<code>Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 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 '@gmail.com' at line 1' in D:\phpStudy\WWW\core\mysql\drivers\PDO.php:61 Stack trace: #0 D:\phpStudy\WWW\core\mysql\drivers\PDO.php(61): PDO->query('SELECT * FROM u...') #1 D:\phpStudy\WWW\core\login.php(105): DB_PDO->getData('SELECT * FROM u...') #2 {main} thrown in D:\phpStudy\WWW\core\mysql\drivers\PDO.php on line 61</code>
登入後複製
登入後複製

回覆內容:

查詢的程式碼是:

<code>$email=htmlspecialchars($_POST["email"]);
if($sql->getData('SELECT * FROM user WHERE email='.$email)==NULL){
</code>
登入後複製
登入後複製

getData的程式碼是:

<code>public function getData($sql,$type=1){
    $data=Array();
    $db=$this->db();

    $result=$db->query($sql);
    $sth = $db->prepare($sql);
    $sth->execute();
    if(is_bool($result))
        return $result;
    if($type==1)
        while($a = $sth->fetch(PDO::FETCH_ASSOC))
            $data[]=$a;
    elseif($type==2)
        while($a = $sth->fetch(PDO::FETCH_BOTH))
            $data[]=$a;

    if($data)
        return $data;
    else
        return NULL;
}
</code>
登入後複製
登入後複製

錯誤提示是:

<code>Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 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 '@gmail.com' at line 1' in D:\phpStudy\WWW\core\mysql\drivers\PDO.php:61 Stack trace: #0 D:\phpStudy\WWW\core\mysql\drivers\PDO.php(61): PDO->query('SELECT * FROM u...') #1 D:\phpStudy\WWW\core\login.php(105): DB_PDO->getData('SELECT * FROM u...') #2 {main} thrown in D:\phpStudy\WWW\core\mysql\drivers\PDO.php on line 61</code>
登入後複製
登入後複製

你需要把email地址引起來

<code class="php"><?php
$email=htmlspecialchars($_POST["email"]);
if($sql->getData('SELECT * FROM user WHERE email="'.$email.'"')==NULL){
</code>
登入後複製

這個是你的 SQL 語句語法錯誤,字串要用引號括起來:

<code class="php">$pdo->getData("SELECT * FROM `table` WHERE `email` = '{$email}'");</code>
登入後複製

以後遇到這種錯誤你印出你的語句出來就知道錯哪了

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板