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.

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

报告相同问题?