note 98975 added to function.substr
| From: | jeffdotswainatpcmmllcdotcom at osu1 dot php dot net | Date: | Tue, 20 Jul 2010 00:00:55 +0000 |
| Subject: | note 98975 added to function.substr | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-170980@lists.php.net to get a copy of this message | ||
I noticed a slight issue when parsing out long strings using the substr function.
Here is my string: $merge = "UPDATE AssistanceRequest SET RequestorID = '4301' WHERE
RequestorID IN (
'4535','6222','4865','5137','4893')"
To parse out the WHERE portion I used:
$whereClause = substr($merge, strpos($merge,'WHERE', (strlen($merge) -
strpos($merge,'WHERE'))));
Normally the function returned: $whereClause = "WHERE RequestorID IN (
'4535','6222','4865','5137','4893')"
This $whereClause gives me the WHERE clause to modify the MSSQL database records being manipulated.
So that when I used $whereClause as the WHERE clause to create subsequent SQL, I used the following
syntax: $setDeleteFlag = "UPDATE AssistanceRequestor SET bIsDirty = 'DELETE' " .
$whereClause;
This should have returned: $setDeleteFlag = "UPDATE AssistanceRequestor SET bIsDirty =
'DELETE' WHERE RequestorID IN (
'4535','6222','4865','5137','4893')"
As long as the length of the original $merge string was less than 104 characters, the $setDeleteFlag
sql came out correctly. However, when the length of the original $merge string exceeded 104
characters, I got this returned:
$setDeleteFlag = "UPDATE AssistanceRequestor SET bIsDirty = 'DELETE' UPDATE
AssistanceRequestor SET bIsDirty = 'DELETE' WHERE RequestorID IN (
'4535','6222','4865','5137','4893')"
The result was that the bIsDirty field for every record in the database was set to
'DELETE'. I fixed it by breaking apart the substr to create the original $whereClause so
that it looked like this:
$wherePosition = strpos($merge,'WHERE');
$whereClause = substr($merge, $wherePosition, strlen($merge) - $wherePosition);
$setDeleteFlag = "UPDATE AssistanceRequestor SET bIsDirty = 'DELETE' " .
$whereClause;
I do have to note that I run PHP 5.x on my development server, while I think the production host is
still on 4.x. I did not seem to have an issue in development, but I don't think I tested
strings longer than 104 characters. Maybe this issue has been corrected in version 5.x.
----
Server IP: 69.147.83.197
Probable Submitter: 74.131.20.254
----
Manual Page -- http://www.php.net/manual/en/function.substr.php
Edit -- https://master.php.net/note/edit/98975
Del: integrated -- https://master.php.net/note/delete/98975/integrated
Del: useless -- https://master.php.net/note/delete/98975/useless
Del: bad code -- https://master.php.net/note/delete/98975/bad+code
Del: spam -- https://master.php.net/note/delete/98975/spam
Del: non-english -- https://master.php.net/note/delete/98975/non-english
Del: in docs -- https://master.php.net/note/delete/98975/in+docs
Del: other reasons-- https://master.php.net/note/delete/98975
Reject -- https://master.php.net/note/reject/98975
Search -- https://master.php.net/manage/user-notes.php