php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #54929 Parse error with single quote in sql comment
Submitted: 2011-05-25 20:00 UTC Modified: 2011-06-05 00:54 UTC
From: hencke at gmail dot com Assigned: felipe (profile)
Status: Closed Package: PDO related
PHP Version: 5.2.17 OS: linux (centos 5.5)
Private report: No CVE-ID: None
 [2011-05-25 20:00 UTC] hencke at gmail dot com
Description:
------------
Using php 5.2.17
Tested PDO with mysql, haven't tried other drivers.
Configure Command =>  './configure'  '--disable-cgi' '--with-mysql' '--enable-pdo' '--with-pdo-mysql' '--with-libdir=lib64'

There is a PDO parsing error when a single quote is inside a block comment. This seems to only occur when there is an input parameter between a commented single quote and any other single quote, either as part of the query or in another comment.

It looks like the single quotes aren't getting ignored in the comment, and cause parameters to not be replaced with their values.

Example SQL that causes a parse error:
/* ' */ select ? as f1, 'foo' as f2
This also causes errors:
/* ' */ select ? as f1 /* ' */



Test script:
---------------
<?
//This query breaks:
$query = "/* ' */ select ? as f1, 'bar' as f2";

//This query also breaks:
//$query = "/* ' */ select ? as f1 /* ' */";

$pdodb = new PDO("mysql:dbname=test;host=127.0.0.1", "testuser", "testuser");
$stmt = $pdodb->prepare($query);
if (!$stmt->execute(array("foo"))) {
	$errInfo = $stmt->errorInfo();
	print_r($errInfo);
}
?>

Expected result:
----------------
The single quotes should be completely ignored when inside comments, and the ? should be replaced with the input parameter value "'foo'".

Actual result:
--------------
The query doesn't execute successfully, with the following PDO error info:
[0] => 42000
[1] => 1064
[2] => You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '? as f1, 'bar' as f2' at line 1


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-05-26 15:18 UTC] johannes@php.net
The parser in PDO is extremely simple. The purpose is to translate place holders according to the DBMS used (some allow ?, some allow :name, some not ...). It is damn simple for doing that. It doesn't know about comments. Comments are complicated as different DBMSes treat them differently. A simple constructed MySQL example:

   SELECT a,b,c FROM t WHERE a>b /*!50122 AND b = ?*/

There the parser has a hard time to guess what happens ...

Not sure there is a _good_ way to properly fix this issue.
 [2011-06-05 00:54 UTC] felipe@php.net
-Summary: Parse error with single quote in sql comment (pdo-mysql) +Summary: Parse error with single quote in sql comment -Status: Open +Status: Closed -Assigned To: +Assigned To: felipe
 [2011-06-05 00:54 UTC] felipe@php.net
This bug has been fixed in SVN.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.


 [2011-06-05 00:54 UTC] felipe@php.net
Automatic comment from SVN on behalf of felipe
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=311812
Log: - Fixed bug #54929 (Parse error with single quote in sql comment)
 [2012-04-18 09:50 UTC] laruence@php.net
Automatic comment on behalf of felipe
Revision: http://git.php.net/?p=php-src.git;a=commit;h=b86161982bf1ee5555e5d7c0ad4b5eed656befcc
Log: - Fixed bug #54929 (Parse error with single quote in sql comment)
 [2012-07-24 23:41 UTC] rasmus@php.net
Automatic comment on behalf of felipe
Revision: http://git.php.net/?p=php-src.git;a=commit;h=b86161982bf1ee5555e5d7c0ad4b5eed656befcc
Log: - Fixed bug #54929 (Parse error with single quote in sql comment)
 [2013-11-17 09:37 UTC] laruence@php.net
Automatic comment on behalf of felipe
Revision: http://git.php.net/?p=php-src.git;a=commit;h=b86161982bf1ee5555e5d7c0ad4b5eed656befcc
Log: - Fixed bug #54929 (Parse error with single quote in sql comment)
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 06:01:30 2024 UTC