php - 应用程序错误 "SQLSTATE[42000]"

标签 php mysql pdo

我构建了 restful API 及其工作,但是当我尝试将参数传递给链接时,下面显示错误,尽管当我打印参数时结果是正确的!

详细信息

Type: PDOException

Code: 42000

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 'where Journal_name like 'Abhinav%' AND is_reported=1' at line 1

File: ...path/index.php

Line: 15

and line 15 is "$stmt = $db->query($sql);"

index.php

<?php

// index.php 

require 'confing.php';
require 'Slim/Slim.php';
\Slim\Slim::registerAutoloader();
$app = new \Slim\Slim();

$app->get('/rout', function() use ($app) {
    $db =getDB();
        $title = $app->request()->params('title');
        // echo 'title ='. $tilte print the correct title name get it by link
    $sql = "SELECT J_name FROM J where where `J_name` like '".$title."%' AND is_reported=1;";
    $stmt = $db->query($sql); 
    $pre = $stmt->fetchAll(PDO::FETCH_OBJ);
        $sql2 = "SELECT P_Name FROM P where `P_Name` like '".$title."%' AND is_reported=1;";
    $stmt2 = $db->query($sql2); 
    $pre2 = $stmt2->fetchAll(PDO::FETCH_OBJ);
    echo json_encode($pre);
    echo json_encode($pre2);
});
$app->run();
?>

最佳答案

在错误的查询中使用两次 where condition

$sql = "SELECT J_name FROM J where where `J_name` like '".$title."%' AND is_reported=1";

                             ^^^^^^^^^^^

删除一个where

应该是

$sql = "SELECT J_name FROM J where `J_name` like '".$title."%' AND is_reported=1";

关于php - 应用程序错误 "SQLSTATE[42000]",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34370226/

相关文章:

php - 页面加载PHP/HTML5随机声音播放器

PHP 类型转换 - 好还是坏?

mysql - Prestashop 在 Wamp 上安装错误创建数据库表

php - PDO误报死锁

PHP - PDO 回显 $array ['name' ];不管用

php - PayPal IPN - 无法接收电子邮件通知

php - 获取刚刚插入数据库的行的唯一ID

php 单击链接以显示基于通过 jquery post() 发送的 id 的 map

mysql - 从 MySQL 安装程序下载 MySQL Server 时失败

php - 使用 switch 元素更新 DB 值