新手希望大家多多指教,用file_get_contents()把sql脚本文件的sql转换成一个字符串,通过截取转换已经得到sql数组。
现在向数据库插入sql语句时出错,为了方便测试,sql脚本文件中只有insert into temp values(1,2);一条语句,字段id,name都为数值型。原语句
$con = mysql_connect("localhost","root","root");
mysql_select_db("kk",$con);
$str = file_get_contents(IN_ROOT.'/mysql.sql');//把整个文件读入一个字符串中
$sqls = sql_split($str); //把字符串拆成自己想要的sql数组
if(is_array($sqls)) {
foreach($sqls as $sql) {
if(trim($sql) != '')
echo $sql; );//打印sql语句
mysql_query($sql,$con);
$num = mysql_affected_rows();//判断插入成功
echo $num;
echo mysql_error(); //打印错误信息
}
}
数据库连接正常,输出结果:insert into temp values(1,2) -1 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'insert into temp values(1,2)' at line 1 第一句是打印的sql语句,-1是 mysql_affected_rows()的返回值,后面是mysql_error()信息。把打印sql语句直接在mysql中操作无误。希望大家多多指教
现在向数据库插入sql语句时出错,为了方便测试,sql脚本文件中只有insert into temp values(1,2);一条语句,字段id,name都为数值型。原语句
$con = mysql_connect("localhost","root","root");
mysql_select_db("kk",$con);
$str = file_get_contents(IN_ROOT.'/mysql.sql');//把整个文件读入一个字符串中
$sqls = sql_split($str); //把字符串拆成自己想要的sql数组
if(is_array($sqls)) {
foreach($sqls as $sql) {
if(trim($sql) != '')
echo $sql; );//打印sql语句
mysql_query($sql,$con);
$num = mysql_affected_rows();//判断插入成功
echo $num;
echo mysql_error(); //打印错误信息
}
}
数据库连接正常,输出结果:insert into temp values(1,2) -1 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'insert into temp values(1,2)' at line 1 第一句是打印的sql语句,-1是 mysql_affected_rows()的返回值,后面是mysql_error()信息。把打印sql语句直接在mysql中操作无误。希望大家多多指教
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货