From: wayne-php at waynef dot com
Operating system: freebsd
PHP version: 4.3.2
PHP Bug Type: Feature/Change Request
Bug description: insert an array with keyed values into a mysql table
i have created a function i found useful. i am sure there are better ways
to doing this and it doesn't do much error checking but you get the idea
and how this may be a useful feature...
/**
** this function will take in an array with keyed values
** and place the values into the
** same name columns as the key name.
**
** bool mysql_insert_array(array, table_name)
**
** ie...
**
** $array = array("id" => $id, "md5" => md5($id));
**
** mysql_insert_array($array, "users");
**/
function mysql_insert_array($array, $table) {
$keys = array_keys($array);
for ($index = 0; $index < count($array); $index++) {
if ($index != 0) {
$columns .= ",";
$values .= ",";
}
/** construct the column names from the array **/
$columns .= $keys[$index];
if (is_int($array[$keys[$index]])) {
$values .= $array[$keys[$index]];
} else {
$values .= "'" .
mysql_real_escape_string($array[$keys[$index]]) . "'";
}
}
$query = "INSERT INTO `" . $table . "` (" . $columns . ") VALUES (" .
$values . ")";
if (($result = mysql_query($query)) == 0)
return(0);
if (mysql_affected_rows() == 1)
return(1);
else
return(0);
}
--
Edit bug report at http://bugs.php.net/?id=24132&edit=1
--
Try a CVS snapshot: http://bugs.php.net/fix.php?id=24132&r=trysnapshot
Fixed in CVS: http://bugs.php.net/fix.php?id=24132&r=fixedcvs
Fixed in release: http://bugs.php.net/fix.php?id=24132&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=24132&r=needtrace
Try newer version: http://bugs.php.net/fix.php?id=24132&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=24132&r=support
Expected behavior: http://bugs.php.net/fix.php?id=24132&r=notwrong
Not enough info: http://bugs.php.net/fix.php?id=24132&r=notenoughinfo
Submitted twice: http://bugs.php.net/fix.php?id=24132&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=24132&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=24132&r=php3
Daylight Savings: http://bugs.php.net/fix.php?id=24132&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=24132&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=24132&r=gnused