#native_company# #native_desc#
#native_cta#

SQL Apostrophe checker

By Paul Broderick
on April 20, 2001

Version: 1.0

Type: Function

Category: Databases

License: Other

Description: Checks the string parameters in a SQL query for single quotes and replaces them with 2 single quotes where necessary.

This is required for MS-SQL where you may need to have an apostrophe in a string.

function checkApostrophes(&$strQuery)	{
	//Remove white space
	$strQuery = trim($strQuery);
	$params = split(",", $strQuery);
	$buffer = "";
	foreach($params as $param)	{
		$param = trim($param);
		if (substr_count($param, "'") > 0)	{
			//replace double all quotes in data
			$param = substr($param, 0, strpos($param, "'"))."'".str_replace("'", "''", substr( $param, strpos($param, "'") + 1, -1 ))."'";
		}
		$buffer .= $param.", ";
	}
	//remove final "," and ""s
	$strQuery = str_replace("", "", substr($buffer, 0,  -2));
}