note 75644 added to ref.array
| From: | sean at aliencreations dot com | Date: | Sat, 09 Jun 2007 23:36:58 +0000 |
| Subject: | note 75644 added to ref.array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-127343@lists.php.net to get a copy of this message | ||
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)";
----
Server IP: 69.147.83.197
Probable Submitter: 76.21.96.242
----
Manual Page -- http://www.php.net/manual/en/ref.array.php
Edit -- https://master.php.net/note/edit/75644
Del: integrated -- https://master.php.net/note/delete/75644/integrated
Del: useless -- https://master.php.net/note/delete/75644/useless
Del: bad code -- https://master.php.net/note/delete/75644/bad+code
Del: spam -- https://master.php.net/note/delete/75644/spam
Del: non-english -- https://master.php.net/note/delete/75644/non-english
Del: in docs -- https://master.php.net/note/delete/75644/in+docs
Del: other reasons-- https://master.php.net/note/delete/75644
Reject -- https://master.php.net/note/reject/75644
Search -- https://master.php.net/manage/user-notes.php