array varaiable to a database field, how?

array varaiable to a database field, how?

am 05.02.2006 05:19:44 von Jim

hi guys and gals,
if i have an array, $details how can i put it in a table database?
please some codes, if it is not too much :D
thanks

Jim

Re: array varaiable to a database field, how?

am 07.02.2006 19:29:29 von nc

Jim S. wrote:
>
> if i have an array, $details how can i put it in a table database?

If you told us what the structure of the array was and how array
elements are mapped to database fields, it would be easier to advise
you on this... Here's a simple example:

$details = array(1 => 'One', 2 => 'Two', 3 => 'Three');
$flag = 0;
$query = 'INSERT INTO tbl_name (id, description) VALUES ';
foreach ($details as $id=>$description) {
if ($flag == 0) {
$query .= " ($id, '$description')";
$flag = 1;
} else {
$query .= ", ($id, '$description')";
}
}
mysql_connect('mysql_host', 'mysql_user', 'mysql_password')
or die('Could not connect: ' . mysql_error());
mysql_select_db('my_database')
or die('Could not select database');
$result = mysql_query($query)
or die('Query failed: ' . mysql_error());

Cheers,
NC