RE: [PHP-DB] inserting mysql data using php array???
| From: | Tim Foster | Date: | Wed, 28 Nov 2001 15:37:03 +0000 |
| Subject: | RE: [PHP-DB] inserting mysql data using php array??? | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-14653@lists.php.net to get a copy of this message | ||
Very interesting. I didn't know you could use SET with an INSERT. I tried this on MySQL
and it worked. Dunno if it's standard SQL, but it didn't work on MSAccess and MS SQLServer
(the only other DBs I have at hand to test it with). I wonder what other DBs it works on,
because you could be painting yourself into a corner if you had to move to another
platform.
TIM
-What if the Hokey-Pokey really is what it's all about?
> -----Original Message-----
> From: Jonathan Hilgeman [mailto:JHilgeman@ecx.com]
> I actually have an easy way to do INSERTs like this.
>
> First I create the array:
>
> $MyArray["field"] = value;
> $MyArray["name"] = "Jonathan";
> $MyArray["age"] = 123;
> $MyArray["email"] = $email_address;
>
> and then I define this function somewhere in a function library:
>
> function Array2Query($dbArray)
> {
> foreach($dbArray as $dbField => $dbValue)
> {
> $dbQuery .= "$dbField='$dbValue',";
> }
>
> $dbQuery = substr($dbQuery,0,strlen($dbQuery)-1);
>
> return $dbQuery;
> }
>
> and then I just create the query:
>
> $Query = "INSERT INTO table SET " . Array2Query($MyArray);
> $Result = mysql_query($Query);
>
> Hope it helps.
>
> - Jonathan