note 75644 deleted from ref.array by felipe
| From: | felipe@php.net | Date: | Sun, 30 Dec 2007 19:29:24 +0000 |
| Subject: | note 75644 deleted from ref.array by felipe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-134530@lists.php.net to get a copy of this message | ||
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)";