Rumah > pembangunan bahagian belakang > tutorial php > mysql - php中使用PDO查询数据库出错

mysql - php中使用PDO查询数据库出错

WBOY
Lepaskan: 2016-09-19 09:16:34
asal
1220 orang telah melayarinya

查询的代码是:

<code>$email=htmlspecialchars($_POST["email"]);
if($sql->getData('SELECT * FROM user WHERE email='.$email)==NULL){
</code>
Salin selepas log masuk
Salin selepas log masuk

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>
Salin selepas log masuk
Salin selepas log masuk

错误提示是:

<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>
Salin selepas log masuk
Salin selepas log masuk

回复内容:

查询的代码是:

<code>$email=htmlspecialchars($_POST["email"]);
if($sql->getData('SELECT * FROM user WHERE email='.$email)==NULL){
</code>
Salin selepas log masuk
Salin selepas log masuk

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>
Salin selepas log masuk
Salin selepas log masuk

错误提示是:

<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>
Salin selepas log masuk
Salin selepas log masuk

你需要把email地址引起来

<code class="php"><?php $email=htmlspecialchars($_POST["email"]);
if($sql->getData('SELECT * FROM user WHERE email="'.$email.'"')==NULL){
</code>
Salin selepas log masuk

这个是你的 SQL 语句语法错误,字符串要用引号括起来:

<code class="php">$pdo->getData("SELECT * FROM `table` WHERE `email` = '{$email}'");</code>
Salin selepas log masuk

以后遇到这种错误你打印你的语句出来就知道错哪了

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan