MySQL插入查询生成器w/字符串
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL插入查询生成器w/字符串相关的知识,希望对你有一定的参考价值。
Takes your table name and key=>value array of values and returns the text for inserting into a mysql database. Automatically surrounds strings with single quotes.
function get_insert_query($table, $array) { $insert_text = "INSERT INTO " . $table; foreach ($array as $k=>$v) { $keys[] = $k; $values[] = $v; } $key_string = "("; foreach ($keys as $key) { $key_string = $key_string . $key . ", "; } $insert_text = $insert_text . " " . $key_string . ")";; $insert_text = $insert_text . " VALUES "; $value_string = "("; foreach ($values as $value) { $value_string = $value_string . "'" . $value . "', "; } else { $value_string = $value_string . $value . ", "; } } $insert_text = $insert_text . $value_string . ")"; return $insert_text; } echo get_insert_query("users", $data); ## Echos: INSERT INTO users (id, name, address) VALUES (23, 'David Lemcoe', '123 Green St.')
以上是关于MySQL插入查询生成器w/字符串的主要内容,如果未能解决你的问题,请参考以下文章
如果记录已经存在或者在 PDO MySQL 插入查询中不存在,如何生成 javascript 警报?
flink学习33:flinkSQL连接mysql,查询插入数据