View Full Version : Insert Array data into MySQL

02-25-2010, 05:30 AM
Hoping someone can help me before all my hair is pulled out. :confused:

I have an array named $arrFeeds:

[0] => Array
[item id] => 9998

I cannot seem to get the code correct to insert the actual value (9998) into the database. I currently just get 0 inserted.

mysql_query("REPLACE INTO items (item_id) VALUES('$arrFeeds[0][item id]') ")
or die(mysql_error());

I can do echo $arrFeeds[0]['item id']; and get the value 9998.

02-25-2010, 06:13 AM
assign it to the mysql as a variable.

$something = $arrFeeds[0]['item id'];

mysql_query("REPLACE INTO items (item_id) VALUES('$something') ")

02-25-2010, 08:51 AM
Try This One

$i = 0;
$sql_query = "SELECT * FROM tablename";
$result = mysql_query($sql_query) or die(mysql_error());
while($row = mysql_fetch_array($result)){

$id = $row[id];
$person = $row[person];

$arr_id[$i] = $id;
$arr_person[$i] = $person;



$smarty->assign("id", $arr_id );

02-25-2010, 01:12 PM
PHP Won't parse array values inside double quoted strings like it will normal variables.

Your code won't work, but this, (or seco's) will:

mysql_query("REPLACE INTO items (item_id) VALUES('{$arrFeeds[0][item id]}') ")

Notice the curly braces I've placed around the array value. That's how you do it.

02-26-2010, 01:23 AM
This code doesn't work either. Results in an error:

Parse error: syntax error, unexpected T_STRING, expecting ']' on line 4

I simplified the original post to have only one key in the inner array. There is actually about 17. So I'll work on a loop making a variable o store the data for each.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum