Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在MS Access PDO查询中绑定日期字符串参数_Php_Sql_Ms Access_Pdo - Fatal编程技术网

Php 在MS Access PDO查询中绑定日期字符串参数

Php 在MS Access PDO查询中绑定日期字符串参数,php,sql,ms-access,pdo,Php,Sql,Ms Access,Pdo,我创建了一个PDO数据库类,用于在MS Access数据库上运行查询。 当使用日期条件进行查询时(在SQL中很常见),日期作为字符串传递。然而,Access通常希望日期用散列括起来。例如 SELECT transactions.amount FROM transactions WHERE transactions.date = #2013-05-25#; 如果使用PDO在何处运行此查询,我可能会执行以下操作 //instatiate pdo connection etc... resultin

我创建了一个PDO数据库类,用于在MS Access数据库上运行查询。 当使用日期条件进行查询时(在SQL中很常见),日期作为字符串传递。然而,Access通常希望日期用散列括起来。例如

SELECT transactions.amount FROM transactions WHERE transactions.date = #2013-05-25#;
如果使用PDO在何处运行此查询,我可能会执行以下操作

//instatiate pdo connection etc... resulting in a $db object
$stmt = $db->prepare('SELECT transactions.amount FROM transactions WHERE transactions.date = #:mydate#;'); //prepare the query
$stmt->bindValue('mydate', '2013-05-25', PDO::PARAM_STR); //bind the date as a string
$stmt->execute(); //run it
$result = $stmt->fetch(); //get the results
根据我的理解,上面的语句看起来是这样的,因为绑定字符串会导致它被引号包围:

SELECT transactions.amount FROM transactions WHERE transactions.date = #'2013-05-25'#;
这会导致错误并阻止语句运行

在PDO中绑定日期字符串而不导致此错误的最佳方法是什么?我目前正在使用
sprintf
-使用字符串,我确信这是一个糟糕的练习

编辑:如果我传递了哈希包围的日期,那么我仍然会得到如下错误:

致命错误:未捕获的异常“PDOException”带有消息 'SQLSTATE[22018]:强制转换规范的字符值无效: -3030[Microsoft][ODBC Microsoft Access驱动程序]条件表达式中的数据类型不匹配。(SQLExecute[-3030]at ext\pdo_odbc\odbc_stmt.c:254)'in C:\xampp\htdocs\ips\php\classes.php:49堆栈跟踪:#0 C:\xampp\htdocs\ips\php\classes.php(49):PDOStatement->execute() C:\xampp\htdocs\ips\php\classes.php(52):数据库->执行() C:\xampp\htdocs\ips\try2.php(12):数据库->结果集() 在第49行的C:\xampp\htdocs\ips\php\classes.php中抛出

您应该在
bindValue
方法中输入日期(整个值)。例如:

$stmt = $db->prepare('SELECT transactions.amount FROM transactions WHERE transactions.date = :mydate'); //prepare the query
$stmt->bindValue('mydate', '#2013-05-25#', PDO::PARAM_STR); //bind the date as a string

通常,当使用准备好的语句或参数化查询时,您不需要担心字符串和日期值的分隔;所有这些都是为你“幕后”处理的

我刚刚尝试了以下方法,效果很好:

<?php
$connStr = 
        'odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};' .
        'Dbq=C:\\Users\\Gord\\Desktop\\Database1.accdb;' .
        'Uid=Admin;Pwd=;';

$dbh = new PDO($connStr);

$sql = 
        "INSERT INTO tblDateTest (dateCol) VALUES (?)";

$newDateTimeValue = "2013-06-30 17:18:19";

$sth = $dbh->prepare($sql);
if ($sth->execute(array($newDateTimeValue))) {
    echo "Done\r\n";
}
else {
    $arr = $sth->errorInfo();
    print_r($arr);
}

好,但这不会导致类似于
…WHERE date='2013-05-25'?我还是要试试。是的,但先看看它是否管用。如果没有,您可能需要运行原始查询,因为您无法从这样准备好的语句中删除引号。它不起作用。见我上面的编辑@GordThompson解决了这个问题,尽管我有预感PDO会处理这个问题,而不需要您提供日期分隔符。很高兴知道并确认。谢谢嗯,是的,我只需要删除所有的引号和散列,现在就可以了。我觉得现在不得不问这个问题有点愚蠢。。。谢谢