dqlxtv1452 2015-10-23 19:11
浏览 40
已采纳

将日期传递给PHP中的ADO命令对象参数

I have code that looks something like this:

$cmd = new com('ADODB.command');
$cmd->activeConnection = $CONNECTION;
$sql = 'UPDATE MyTable SET WhenPlanned = ? WHERE ID = ?';
$cmd->commandText = $sql;
$p1 = $cmd->createParameter('WhenPlanned', adDate, adParamInput, 0, $dateVal)
$p2 = $cmd->createParameter('ID', adInteger, adParamInput, 4, $intVal); 
$cmd->parameters->append($p1);
$cmd->parameters->append($p2);
$cmd->execute();

This only produces the following error: PHP Fatal error: Uncaught exception 'com_exception' with message 'Source: ADODB.Command Description: Application uses a value of the wrong type for the current operation.'

The $dateVal variable is a string representation of a date. I've tried creating a PHP datetime object to pass to no avail...Does anyone know how to pass in a date that the ADO Parameter will accept as an argument in PHP? Thanks!

  • 写回答

1条回答 默认 最新

  • doumiang0597 2015-10-28 16:31
    关注

    I created a test table to isolate and experiment with date parameters and found something that worked. I guess it's a date formatting issue. I was passing in dates that looked like the string in the date() function below without formatting.

    $cmd = new com('ADODB.command');
    $cmd->activeConnection = $CONNECTION[SYSTEM];
    $sql = 'INSERT INTO TestDateTable (testdate) VALUES (?)';
    $cmd->commandText = $sql;
    $d = date('Y-m-d H:i:s', strtotime('10/26/2015'));
    $p1 = $cmd->createParameter('testdate', adDate, adParamInput, 0,  $d);
    $cmd->parameters->append($p1);
    $cmd->execute();
    

    This successfully inserted a date in my table.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程