Hey all,

Im a little confused about taking the values of a 2D array and placing them into a query or string to use in query. I've done some homework but unfortunatly square eyes and a caffeine comedown just isn't helping.

Essentially I have an array named $myarray.
It looks like this:
Array ( [1] => 3 [2] => 4 [3] => 3 )
What I'd like to do is take the 3,4,3, and be able to use in a sql query such as "INSERT INTO table(column) VALUES (3, 4, 3)";

Any help will be welcomed as most sites I've checked refer to using implode on 1D arrays.

8 Years
Discussion Span
Last Post by Exploded Fiber

If you want to insert into different columns different values then try the following.

$array=array('column1'=>'value1', 'column2'='value2', 'column'='another');
$sql='INSERT INTO `table` SET';
foreach ($array AS $key => $val) {
$sql.=' `'.mysql_real_escape_string($key).'` = "'.mysql_real_escape_string($val).'",';

echo $sql;

Hey cwarn23,

Thanks for your reply. I took on board your code, however, went with something like this instead.

while($row= mysql_fetch_array($result)){
			$myarray= $row[1];
			$text.=(" '".$myarray."',");

Appreciate the input. As I'm failry new to php it gave me a chance to examine "mysql_real_escape_string"
Thanks again.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.