Re: confused big time

From: Date: Wed, 07 Apr 2004 16:27:47 +0000
Subject: Re: confused big time
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-182672@lists.php.net to get a copy of this message
From: "Mark Ackroyd" <mark@ackroyd.net> > I always found this way of inserting data into a database messy. Here is > a handy function to do array inserts and it builds the sql for you. > > function arrayINSERT($a,$tablename) > { > $sql = "INSERT INTO $tablename ("; > foreach($a as $key => $value) > { > $sql .= $key .","; > } > $sql[strlen($sql)-1] = ')'; > $sql .= " VALUES ("; > foreach($a as $key => $value) > { > if (gettype($value) == 'string') > { > $sql .= "'". addslashes($value) ."',"; > } > else > { > $sql .= $value .","; > } > } > $sql[strlen($sql)-1] = ')'; > return $sql; > } To get your list of columns, you could do this: $column_list = implode(',',array_keys($a)); $sql = "INSERT INTO $tablename ($column_list) VALUES "; That way you only have to loop through $a once. ---John Holmes...

« previous php.general (#182672) next »