【问题标题】:Why is this SQL SELECT statement not retrieving the row I just inserted?为什么这个 SQL SELECT 语句没有检索到我刚刚插入的行?
【发布时间】:2011-11-28 16:26:33
【问题描述】:

我正在创建一个内存 SQLite 数据库并在其中插入一些行。不幸的是,我无法选择刚刚插入的行。

<?php

const CACHE_SCHEMA = <<<EOD

CREATE TABLE test ( column1 TINYTEXT,
                    column2 MEDIUMTEXT,
                    column3 INT,
                    column4 INT )

EOD;

$db = new PDO('sqlite::memory:');

$statement = $db->query(CACHE_SCHEMA);
$statement->execute();

$statement = $db->prepare('INSERT INTO test (column1, column2, column3, column4) VALUES (?,?,?,?)');
$statement->execute(array('a', 'b', 123, 2));

$statement = $db->prepare('SELECT column2 FROM test WHERE column1 = ? AND column3 + column4 >= ?');
$statement->execute(array('a', 124));

var_dump($statement->fetch(PDO::FETCH_ASSOC));

?>

输出: bool(false)

如您所见,'SELECT' 语句没有返回我插入的行:

  • 在我插入的行中添加'column3''column4' 会产生125
  • 语句的'WHERE'子句变为:

    'WHERE column1 = "a" AND column3 + column4 &gt;= 124'

    一旦参数被填写。

  • 除非我弄错了,否则125 &gt;= 124

【问题讨论】:

    标签: php sql sqlite pdo


    【解决方案1】:

    想了想解决这个问题,但根据the documentation for PDOStatement::execute()

    “所有值都被视为 PDO::PARAM_STR。”

    所以我需要改变:

    $statement->execute(array('a', 124));
    

    到:

    $statement->bindValue(1, 'a');
    $statement->bindValue(2, 124, PDO::PARAM_INT);
    $statement->execute();
    

    ...然后不是将字符串值绑定到最后一个参数,而是绑定一个整数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-04-12
      • 1970-01-01
      • 2014-08-23
      • 2022-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多