note 69320 added to function.mysql-fetch-assoc
| From: | Sergiu at osu1 dot php dot net | Date: | Thu, 31 Aug 2006 14:01:21 +0000 |
| Subject: | note 69320 added to function.mysql-fetch-assoc | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-116543@lists.php.net to get a copy of this message | ||
A function, fully commented, for inserting an associative array into a table.
<?php
function mysql_insert_assoc ($my_table, $my_array) {
// Find all the keys from the array $my_array
$keys = array_keys($my_array);
// Find the number of the keys
$keys_number = count($keys);
// Generate the column name for the $sql query
$column_names = "(";
$values = "(";
for ($i = 0; $i < $keys_number; $i++) {
$column_name = $keys[$i];
// We don't add "," after the last one
if ($i == ($keys_number - 1)) {
$column_names .= "
$keys[$i]";
$values .= "'$my_array[$column_name]'";
} else {
$column_names .= "$keys[$i]" . ", ";
$values .= "'$my_array[$column_name]'" . ", ";
}
}
// Proper end the column name and the value
$column_names .= ")";
$values .= ")";
// We compose the query
$sql = "insert into $my_table ";
$sql .= $column_names;
$sql .= ' values ';
$sql .= $values;
$result = mysql_query($sql);
if ($result)
{
echo "The row was added sucessfully";
return true;
}
else
{
echo ("The row was not added<br>The error was" . mysql_error());
return false;
}
}
?>
----
Server IP: 66.163.161.117
Probable Submitter: 84.232.205.115
----
X-Spam-Status: No, hits=3.3 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER,
HTML_MESSAGE autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/function.mysql-fetch-assoc.php
Edit -- https://master.php.net/note/edit/69320
Del: integrated -- https://master.php.net/note/delete/69320/integrated
Del: useless -- https://master.php.net/note/delete/69320/useless
Del: bad code -- https://master.php.net/note/delete/69320/bad+code
Del: spam -- https://master.php.net/note/delete/69320/spam
Del: non-english -- https://master.php.net/note/delete/69320/non-english
Del: in docs -- https://master.php.net/note/delete/69320/in+docs
Del: other reasons-- https://master.php.net/note/delete/69320
Reject -- https://master.php.net/note/reject/69320
Search -- https://master.php.net/manage/user-notes.php