今天做了一下自动备份MYSQL数据库,遇到了一个符号问题,for ($i = 0;$i < $_numfields;$i++ ) {
$_tabledump .= "INSERT INTO `$_table` VALUES(";
$_tabledump .= "'"mysql_escape_string($_row[$i])."',";}
$_tabledump .= ");\n";
用以上代码,出来的SQL代码如下:
最后一个字段'2011-09-28 18:25:21' 也有 , 号,怎么将其取消啊? 谢谢
INSERT INTO `tg_category` VALUES('1','无显','范超','2011-09-28 18:25:21',);
$_tabledump .= "INSERT INTO `$_table` VALUES(";
$_tabledump .= "'"mysql_escape_string($_row[$i])."',";}
$_tabledump .= ");\n";
用以上代码,出来的SQL代码如下:
最后一个字段'2011-09-28 18:25:21' 也有 , 号,怎么将其取消啊? 谢谢
INSERT INTO `tg_category` VALUES('1','无显','范超','2011-09-28 18:25:21',);
$_tabledump .= "INSERT INTO `$_table` VALUES(";
$_tabledump .= "'"mysql_escape_string($_row[$i])."',";}
rtrim($_tabledump, ',');
$_tabledump .= ");\n";
$_tabledump .= "INSERT INTO `$_table` VALUES(";
$_tabledump .= "'"mysql_escape_string($_row[$i])."',";}
$_tabledump = rtrim($_tabledump, ',');
$_tabledump .= ");\n";
string rtrim ( string $str [, string $charlist ] )当然也可以用各种其他函数来处理字符串.
for ($i = 0;$i < $_numfields;$i++ ) {
$_tabledump .= "'"mysql_escape_string($_row[$i])."',";}
$_tabledump = preg_replace("/,\s*?$/",'',$_tabledump).");\n";
方法二$v = array()
for ($i = 0;$i < $_numfields;$i++ ) {
$v[].= "'"mysql_escape_string($_row[$i])."'";}
$_tabledump .= "INSERT INTO `$_table` VALUES(".implode(',',$v).");\n";
加个判断
if($_row[$i])!=end($_row){
$_tabledump .= "'"mysql_escape_string($_row[$i])."',";}
else
$_tabledump .= "'"mysql_escape_string($_row[$i])."'";
$_tabledump .= "INSERT INTO `$_table` VALUES(";
for ($i = 0;$i < $_numfields;$i++ ) {
$_tabledump .= "$ch'".mysql_escape_string($_row[$i])."'";
$ch = ',';
}
$_tabledump .= ");\n";