我有两个具有精确表结构(table1)的数据库($db1,$db2)。$db2.table1 有我想插入到 $db1.table1 的新行(即,如果 $db2 是新的而 $db1 是旧的,我想用 $db2 中的新条目更新 $db1)。
我想出了以下 php 代码,它应该可以正常工作,但我担心列 id 中的特殊字符以及要插入其中的值。
这是代码:
require('update_functions.php'); //contains helper functions
function arraywalk_mysql_real_escape_string(&$value, &$key, $sql_connection) {
$value = mysql_real_escape_string($value, $sql_connection);
$key = mysql_real_escape_string($key, $sql_connection);
}
$sql_connection_old = connectdb('old');
$sql_connection_new = connectdb('new');
$table = 'member'; $pkey = 'id'; //table name and primary key
$last_row_member = mysql_fetch_assoc(last_row($sql_connection_old, $table, $pkey));
//fetches last row from old db
$new_row = new_row($sql_connection_new, $pkey, $last_row_member[$pkey], $table, 'ASC LIMIT 1');
//the new_row function executes following query (after sanitizing input)
//'SELECT * FROM '.$table.' WHERE '.$pkey.'>'.$pkey_value.' ORDER BY '.$pkey.' '.$extra
while($result = mysql_fetch_assoc($new_row)) {
array_walk($result, 'arraywalk_mysql_real_escape_string', $sql_connection_old);
$update_member_query = 'INSERT INTO ' . $table . '( '
. implode(", ", array_keys($insert_vars))
. ' ) VALUES ( \''
. implode("', '", $insert_vars)
. '\' )';
}
我不知道列名中是否会有任何特殊字符。我应该用反引号将它们括起来吗?
如果是,那么我应该使用解析它们mysql_real_escape_srting()
吗?
价值观呢?我应该用引号将它们括起来'value'
吗?如果要插入的值是一个数字怎么办?如果它的日期/时间怎么办?
有没有一种方法可以绕过从旧数据库中获取数据到 PHP 变量中并将其插入回数据库(因此上述问题变得无关紧要)?
注意:即使有两个连接,我也有相同的 SQL 服务器为两个 $db 服务