note 69666 added to function.mysql-fetch-assoc
| From: | R dot Bradley at osu1 dot php dot net | Date: | Thu, 14 Sep 2006 15:34:19 +0000 |
| Subject: | note 69666 added to function.mysql-fetch-assoc | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-117159@lists.php.net to get a copy of this message | ||
In response to Sergiu's function - implode() would make things a lot easier ... as below:
<?php
function mysql_insert_assoc ($my_table, $my_array) {
// Find all the keys (column names) from the array $my_array
$columns = array_keys($my_array);
// Find all the values from the array
$values = array_values($my_array);
// We compose the query
$sql = "insert into
$my_table ";
// implode the column names, inserting "\", \"" between each (but not
after the last one)
// we add the enclosing quotes at the same time
$sql .= "(\"" . implode("\", \"", $column_names) .
"\")";
$sql .= " values ";
// Same with the values
$sql .= "(" . implode(", ", $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;
}
}
?>
Thus, a call to this function of:
mysql_insert_assoc("tablename", array("col1"=>"val1",
"col2"=>"val2"));
Sends the following sql query to mysql:
INSERT INTO tablename ("col1", "col2") VALUES ("val1",
"val2")
----
Server IP: 216.235.15.211
Probable Submitter: 70.48.254.84
----
Manual Page -- http://www.php.net/manual/en/function.mysql-fetch-assoc.php
Edit -- https://master.php.net/note/edit/69666
Del: integrated -- https://master.php.net/note/delete/69666/integrated
Del: useless -- https://master.php.net/note/delete/69666/useless
Del: bad code -- https://master.php.net/note/delete/69666/bad+code
Del: spam -- https://master.php.net/note/delete/69666/spam
Del: non-english -- https://master.php.net/note/delete/69666/non-english
Del: in docs -- https://master.php.net/note/delete/69666/in+docs
Del: other reasons-- https://master.php.net/note/delete/69666
Reject -- https://master.php.net/note/reject/69666
Search -- https://master.php.net/manage/user-notes.php