[thelist] MySQL/PHP Associative Array Insert
Anthony Baratta
Anthony at Baratta.com
Sat Feb 1 10:59:01 CST 2003
At 11:44 PM 1/31/2003, CDitty wrote:
>A co-worker is teaching me to move to the next level in php. I have
>started using associative arrays for my scripts, but I am having a problem
>using them to do an insert into MySQL. Can someone give me an example of
>how to do an insert to the database using these arrays? My array is
>this...$item['itemID']
It's no different that using a regular array:
$strSQL = "INSERT INTO Table ";
$strSQL =. "(Col1, Col2, Col3, Col4) ";
$strSQL =. "VALUES (";
$strSQL =. " '" . $item['Col1'] . "'";
$strSQL =. ",'" . $item['Col2'] . "'";
$strSQL =. ",'" . $item['Col3'] . "'";
$strSQL =. ",'" . $item['Col4'] . "'";
$strSQL =. ")";
<regular db execute code here>
Another way would be to use loops:
(Assuming your keys are named the same as your columns.)
psuedo code alert
$varKeys = Keys($item);
foreach $Fld ($varKeys) {
if ($strSQL1 ne "") {$strSQL1 =. ","}
if ($strSQL2 ne "") {$strSQL2 =. ","}
$strSQL1 =. $Fld;
$strSQL2 =. "'" . $item[$Fld] . "'";
}
$strSQL = "INSERT INTO Table ";
$strSQL =. "(" . strSQL1 . ")";
$strSQL =. "VALUES (" . strSQL2 . ")";
<regular db execute code here>
---
Anthony Baratta
President
Keyboard Jockeys
"Conformity is the refuge of the unimaginative."
More information about the thelist
mailing list