note 75644 deleted from ref.array by felipe

[email protected]
Newsgroups php.notes
Message-ID <[email protected]>
Note Submitter: sean at aliencreations dot com 

----

I wrote this function to parse arrays so they work with SQL strings if you need to use the MySQL "IN()" function.

function format_sql_array($array)
{
	$SQLstring = "";
	foreach($array as $item)
	  {
		$SQLstring .= "'$item',";
	  }
	$SQLstring = rtrim($SQLstring, ",");
	$SQLstring = str_replace("'',", "", $SQLstring);
	return $SQLstring;
}

Example: 
$my_array = array("red", "blue", "green");
$sql_array = format_sql_array($my_array);
//$sql_array is now "'red','blue','green'"

Sample SQL:
$SQL = "SELECT FROM colors_table WHERE color IN($sql_array)";
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.