php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #40452 ODBC and SQL Server datetime parameter fields fail on insert
Submitted: 2007-02-12 21:14 UTC Modified: 2009-05-03 01:00 UTC
Votes:30
Avg. Score:4.6 ± 0.7
Reproduced:26 of 27 (96.3%)
Same Version:7 (26.9%)
Same OS:20 (76.9%)
From: aspen dot olmsted at alliance dot biz Assigned: wez (profile)
Status: No Feedback Package: PDO related
PHP Version: 5.2.1 OS: Windows 2003, XP
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2007-02-12 21:14 UTC] aspen dot olmsted at alliance dot biz
Description:
------------
If you have MS SQL datetime field using odbc and try to use parameterized inserts you get a sql sequence error.

Reproduce code:
---------------
Here is sql to create table

CREATE TABLE [ASC_EventPreSale] (
	[apsEventCode] [int] NOT NULL CONSTRAINT [DF_ASC_EventPreSale_apsEventCode] DEFAULT (0),
	[apsStartDate] [datetime] NOT NULL CONSTRAINT [DF_ASC_EventPreSale_apsStartDate] DEFAULT (getdate()),
	[apsFinishDate] [datetime] NOT NULL CONSTRAINT [DF_ASC_EventPreSale_apsFinishDate] DEFAULT (getdate()),
	[apsGeneralPublicDate] [datetime] NOT NULL CONSTRAINT [DF_ASC_EventPreSale_apsGeneralPublicDate] DEFAULT (getdate()),
	CONSTRAINT [PK_ASC_EventPreSale] PRIMARY KEY  CLUSTERED 
	(
		[apsEventCode]
	)  ON [PRIMARY] 
) ON [PRIMARY]
GO


Here is php
<?php
$sql ="INSERT INTO ASC_EventPreSale (apsEventCode,apsStartDate,apsFinishDate,apsGeneralPublicDate) VALUES (:apsEventCode,:apsStartDate,:apsFinishDate,:apsGeneralPublicDate)";

$avalues['apsEventCode'] = 6339;
$avalues['apsStartDate'] = '2007-02-12 14:5';
$avalues['apsFinishDate'] = '2007-02-12 16:5';
$avalues['apsGeneralPublicDate'] = '2007-02-15 14:5';

$stmt=$dbh->prepare($sql);
$stmt->execute($avalues);
?>

Expected result:
----------------
If you change the field types to varchar it works fine.  It should function the same with date time fields

Actual result:
--------------
SQL Error

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-02-12 21:27 UTC] tony2001@php.net
Thank you for this bug report. To properly diagnose the problem, we
need a short but complete example script to be able to reproduce
this bug ourselves. 

A proper reproducing script starts with <?php and ends with ?>,
is max. 10-20 lines long and does not require any external 
resources such as databases, etc. If the script requires a 
database to demonstrate the issue, please make sure it creates 
all necessary tables, stored procedures etc.

Please avoid embedding huge scripts into the report.


 [2007-02-12 21:43 UTC] aspen dot olmsted at alliance dot biz
I included a small script in the reproduce code?
 [2007-02-12 21:54 UTC] tony2001@php.net
But you forgot the error itself.
And I would appreciate if you include a working script, too.
 [2007-02-12 22:07 UTC] aspen dot olmsted at alliance dot biz
Here is the error:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY010]: Function sequence error: 0 [Microsoft][ODBC Driver Manager] Function sequence error (SQLExecute[0] at ext\pdo_odbc\odbc_stmt.c:133)' in C:\Program Files\nusphere\phped\Projects\ASCPlatform\noname1.php:11 Stack trace: #0 C:\Program Files\nusphere\phped\Projects\ASCPlatform\noname1.php(11): PDOStatement->execute(Array) #1 {main} thrown in C:\Program Files\nusphere\phped\Projects\ASCPlatform\noname1.php on line 11

The only thing missing from the script is:
    $dbh = new PDO('odbc:peo', '11', '11');
 [2007-02-12 22:22 UTC] tony2001@php.net
Are you sure "2007-02-12 14:5" is correct timestamp for MSSQL?
Doesn't it lack the last symbol?
 [2007-02-13 01:31 UTC] aspen dot olmsted at alliance dot biz
Yes.  If you insert into sql other ways it works perfect.  Including through php
 [2007-04-24 19:00 UTC] daniel dot tams at gmail dot com
I can confirm this problem on PHP 5.2.1. The exact error is the following: 
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY010]: Function sequence error: 0 [Microsoft][ODBC Driver Manager] Function sequence error (SQLExecute[0] at ext\pdo_odbc\odbc_stmt.c:133)' in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\iptaa\lib\IPTAA_CRUD.class.php:135 Stack trace: #0 C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\iptaa\lib\IPTAA_CRUD.class.php(135): PDOStatement->execute() #1 C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\iptaa\lib\IPTAA_CRUD.class.php(414): IPTAA_CRUD->create(Array) #2 C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\iptaa\users.php(15): IPTAA_CRUD->handleHttpPost() #3 {main} thrown in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\iptaa\lib\IPTAA_CRUD.class.php on line 135

Fatal error: Exception thrown without a stack frame in Unknown on line 0

This is a pretty serious limitation of the PDO ODBC driver.
 [2007-04-24 19:05 UTC] daniel dot tams at gmail dot com
Just wanted to clarify that the error is unrelated to the format of the datetime.
 [2007-10-11 14:22 UTC] jfdsmit at gmail dot com
I have a similar experience, running PHP 5.2.4 on Windows 2003. The server /does/ accept values when they are formatted exactly yyyy-mm-dd (as per the date() format Y-m-d) /or/ if the supplied value == NULL. Empty strings and dates formatted otherwise (2007-3-12 for instance) result in a HY010 SQLSTATE Sequence error. So contrary to what daniel dot tams at gmail dot com says, the bug is formatting-dependant in my experience
 [2007-11-23 11:04 UTC] rob at tdd dot org dot uk
I was experiencing the same problem with a stored procedure (SQL SERVER), however I found that I was calling the parameters in a different order in which they where expected, even though I was specifying the parameter name.

-- replcate

CREATE PROC hello
@world VARCHAR(100),
@repeat INT
AS

	SELECT 'hello ' + REPLICATE(@world, @repeat)

GO

-- both give same output

EXEC hello @world = 'world', @repeat = 5
EXEC hello @repeat = 5, @world = 'world'


If I was to call the first exec equivalent in php using bound parameters then it works, however the php equivalent using bound parameters it fails with a function sequence error.
 [2008-02-29 20:11 UTC] ethan dot nelson at ltd dot org
This code illustrates the fact that SQL can work with a variety of date formats when moving to a SQL2005 type of datetime.

To use this script, remove the sql comments '--' and comment out the appropriate params line to test PDO attempts to work with the different formats.  All of them throw exceptions.

<?php

include("../include/config.inc");

$db = new bkg_db();

$query = "SELECT 'sqltest1' = CAST('20080101 12:07:32' AS datetime)
,'sqltest2' = CAST('2008-01-01 12:07:32' AS datetime)
,'sqltest3' = CAST('01/01/2008 12:07:32' AS datetime)
,'sqltest4' = CAST('01-01-2008 12:07:32' AS datetime)
--,'pdotest' = CAST(:pdotest AS varchar)
";

$params = array(':pdotest' => '20080101 12:07:32');
$params = array(':pdotest' => '2008-01-01 12:07:32');
$params = array(':pdotest' => '01/01/2008 12:07:32');
$params = array(':pdotest' => '01-01-2008 12:07:32');
$params = array();

$db->execute_query($query,$params);

print_r($db->get_results());

?>
 [2008-02-29 22:20 UTC] ethan dot nelson at ltd dot org
This bug may be due to failure of SQL Server to perform implicit conversion between character and datetime data types when the characterlength is not explicitly part of variable declaration.  I posted my workaround under PDO_ODBC section.
 [2008-11-19 17:19 UTC] rolfpinto at hotmail dot com
this is code is in php:
<?php
...
$date = '12/01/2007';
mssql_bind($srtproc, "@somedate", $date, SQLVARCHAR, FALSE, FALSE);
...
?>

this is code in your store procedure under ms sql server
ALTER PROCEDURE [dbo].[sp_agregaranimal]
@somedate DATETIME,
AS 
BEGIN
SELECT CONVERT(datetime, @somedate, 102) 
END
 [2009-04-14 16:49 UTC] 0kph at 0kph dot com
This error: 

SQLSTATE[HY010]: Function sequence error: 0 [Microsoft][ODBC Driver
Manager] Function sequence error (SQLExecute[0] at ext\pdo_odbc\odbc_stmt.c:133) 

throws always when You bind value long than field in table.

In fact, error disapear, when you cut value or extend field in table.

/* c is field varchar(5) */
$sql = 'SELECT a FROM b WHERE c = ?';
$stmt = $pdo->prepare($sql);
/* @var $stmt PDOStatement */
$stmt->bindValue(1, '1234567890');//10 chars
$stmt->execute();

When You will change field c to varchar(10) - error disapear.

I hope, it help in coding with ODBC.
 [2009-04-25 14:43 UTC] jani@php.net
Please try using this CVS snapshot:

  http://snaps.php.net/php5.2-latest.tar.gz
 
For Windows:

  http://windows.php.net/snapshots/


 [2009-05-03 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a week, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
 [2009-10-28 18:43 UTC] siromega at gmail dot com
I am still experiencing this problem at 5.2.11 Windows XP and 2003, MS SQL Server 2005. PHP PDO, ODBC driver. I am providing a test case that meets the requirements because this issue does not appear to be resolved. 

<?php

$activeConnection = new PDO("odbc:mssql_dev", "phpbug","phpbug");

$stmt = $activeConnection->prepare("create table phpbug (id int, dt datetime)");
$stmt->execute();

$stmt = $activeConnection->prepare("insert into phpbug (id, dt) values (:id, :dt)");
$stmt->bindValue(":id", 1, PDO::PARAM_INT);
$stmt->bindValue(":dt", "01-JAN-09", PDO::PARAM_STR); // There is no PDO::PARAM_DATE
if (!$stmt->execute()) 
  print_r($stmt->errorInfo());

?>

Error: [Microsoft][ODBC Driver Manager] Function sequence error (SQLExecute[0] at ext\pdo_odbc\odbc_stmt.c:133)
 [2009-12-07 12:04 UTC] php at movsisyan dot com
Hi guys, I faced to this problem some time ago when switched to odbc driver from native PDO mssql driver because have some problems with second one.
After spending some time to find solution I saw that problem with datetime format - ODBC do not accept standard date format that acceptable by other PDO drivers. In insert or update queries use this format: date('Ymd'), this worked for me and I hope that be useful for others.

MSSQL 2005
PHP 5.2.9
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Apr 20 06:01:28 2024 UTC