PHP:使用数组[MySQL][PHP][Array][Insert]将数据插入MySQL表

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP:使用数组[MySQL][PHP][Array][Insert]将数据插入MySQL表相关的知识,希望对你有一定的参考价值。

Inserts the values of an array into a table. Also supports specifying if a specific field needs to be encoded using PASSWORD()
Parameters:
Table: Name of table to insert into
Data: array of $field->$value of new data
Password Field: Which field in the data array needs to be surrounded with PASSWORD() (optional)
  1. function mysql_insert_array($table, $data, $password_field = "") {
  2. foreach ($data as $field=>$value) {
  3. $fields[] = '`' . $field . '`';
  4.  
  5. if ($field == $password_field) {
  6. $values[] = "PASSWORD('" . mysql_real_escape_string($value) . "')";
  7. } else {
  8. $values[] = "'" . mysql_real_escape_string($value) . "'";
  9. }
  10. }
  11. $field_list = join(',', $fields);
  12. $value_list = join(', ', $values);
  13.  
  14. $query = "INSERT INTO `" . $table . "` (" . $field_list . ") VALUES (" . $value_list . ")";
  15.  
  16. return $query;
  17. }

以上是关于PHP:使用数组[MySQL][PHP][Array][Insert]将数据插入MySQL表的主要内容,如果未能解决你的问题,请参考以下文章

PHP数组函数

PHP 5 Array 函数

PHP - 在多维数组中查找键

php数组

PHP学习记录

PHP数组调试