php - PDO 调用在 PHP 中失败,但在 MySQL Workbench 中成功执行

标签 php mysql pdo

我正在通过 PDO 连接到 MySQL 数据库,但是当我尝试在 PHP 中执行 SQL 调用时,它失败并出现以下错误。当我回显 SQL 并在 MySQL Workbench 中运行它时,它返回的结果正是我所期望的。这是 $prepared->errorInfo() 失败时的输出:

Array (
    [0] => 42000
    [1] => 1064
    [2] => 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 "5" at line 17
)

所以看起来准备好的语句以及在执行时向其传递值有问题,但我不确定是什么。 http://dev.mysql.com/doc/refman/5.5/en/error-messages-server.html#error_er_parse_error除了告诉我同样的问题之外并没有真正的帮助。

function __construct($params = array()) {

    $DB = new PDO('mysql:host='.$config['host'].';port='.$config['port'].';dbname='.$config['name'], $config['user'], $config['pswd']);
    if (!$DB) {
        throw new Exception('Could not connect to database');
    }

    $type = $params['topic'];

    if (isset($params['topic']) && $params['topic'] !== '') {
        $typeSlugSql = "SELECT id from topics WHERE slug = ? LIMIT 1";

        $prepared = $DB->prepare($topicSlugSql);
        $list = $prepared->execute(array($params['topic'])); //NOTE: this is working correctly and returns the ID I expect
        if (!$list) {
            throw new Exception('topic slug execute failed');
        }
        $row = $prepared->fetch(PDO::FETCH_ASSOC);

        $topic = $row['id']; //id is correctly 6 at this point
    }

    //generate SQL query
    $sql = "SELECT
                articles.id AS id,
                articles.slug AS slug,
                articles.title AS title,
                articles.headline AS headline,
                articles.description AS description,
                articles.keywords AS keywords,
                articles.content AS content,
                DATE(articles.published_date) AS date,
                articles.created AS created
            FROM
                articles articles
            WHERE
                articles.published_date <= CURRENT_TIMESTAMP
                AND (articles.published IS NOT NULL OR articles.published = 1)
            ORDER BY
                articles.published_date DESC LIMIT 0 , ?";

    //run the query and get the feed data
    $prepared = $DB->prepare($sql);
    $items = $prepared->execute(array(5)); //this will make the SQL fail...

    echo $sql;

    if (!$items) {
        print_r($DB->errorInfo()); //this prints the 'errorInfo' listed above
        throw new Exception('execution error...');
    }

    $options = array('ellipsis' => '...', 'exact' => false, 'html' => false);
    while ($row = $prepared->fetch(PDO::FETCH_ASSOC)) {
        array_push($this->resources, new Resource($row['title'], $row['slug'], $row['date'], $this->cleanAndTruncate($row['content'])));
    }

}

最奇怪的部分是 $prepared->execute(array($params['type'])) 正常工作并返回我期望的 ID。同样,如果我复制/粘贴 echo 的 SQL 并将 ? 替换为 5,它会在 MySQL Workbench 中按预期工作。如果我这样做:

    $sql = "SELECT
                articles.id AS id,
                articles.slug AS slug,
                articles.title AS title,
                articles.headline AS headline,
                articles.description AS description,
                articles.keywords AS keywords,
                articles.content AS content,
                DATE(articles.published_date) AS date,
                articles.created AS created
            FROM
                articles articles
            WHERE
                articles.published_date <= CURRENT_TIMESTAMP
                AND (articles.published IS NOT NULL OR articles.published = 1)
            ORDER BY
                articles.published_date DESC LIMIT 0 , 5";

    $prepared = $DB->prepare($sql);
    $items = $prepared->execute();

它适用于 PDO 和 MySQL Workbench,但我需要根据传入的参数更改 LIMIT。有什么建议可以找出是什么破坏了这个吗?

最佳答案

试试这个:

$sql = "SELECT
            articles.id AS id,
            articles.slug AS slug,
            articles.title AS title,
            articles.headline AS headline,
            articles.description AS description,
            articles.keywords AS keywords,
            articles.content AS content,
            DATE(articles.published_date) AS date,
            articles.created AS created
        FROM
            articles articles
        WHERE
            articles.published_date <= CURRENT_TIMESTAMP
            AND (articles.published IS NOT NULL OR articles.published = 1)
        ORDER BY
            articles.published_date DESC LIMIT :limit , 5";

$stmt = $DB->prepare($sql);

$limit = 5;

$stmt->bindParam(':limit', $limit, PDO::PARAM_INT);

$items = $stmt->execute();

关于php - PDO 调用在 PHP 中失败,但在 MySQL Workbench 中成功执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27554309/

相关文章:

php - 无法在存储过程 phpmyadmin 中声明变量 | mysql

MySQL 中日期和 BETWEEN 的奇怪之处

php - 获取准备好的语句中最后插入的 ID

javascript - 记住跨多个页面的 javascript 变量

php - 升级具有良好打印支持的 VB6 应用程序的建议

javascript - 使用ajax发送表单数据

MySql 查询复杂的选择

mysql - PDO 和行数

php - 在一张 table 上选择不同的作品,但在另一张 table 上不选择不同的作品

php - 自定义 html Wordpress 评论,带头像和回复