假设现在有条update语句,有时候update语句正确,但是受影响的行数是0。
那么怎么判断这条SQL语句到底是否正确?使用 mysqli_error($Conn);
create table user( name varchar(20), age int(5), hobby text);
mysql> insert into user values('zhangsan',11,'test....'); Query OK, 1 row affected (0.01 sec) mysql> update user set name='zhangsan'; Query OK, 0 rows affected (0.00 sec) Rows matched: 1 Changed: 0 Warnings: 0 mysql> update user set username='zhangsan'; ERROR 1054 (42S22): Unknown column 'username' in 'field list'
要在PHP中区分上面 第2条 和 第3条SQL语句,使用如下代码:
$conn=mysqli_connect($server_name, $username, $password, $database, $port);
$sql = "update user set name='zhangsan'";
$res = mysqli_query($conn,$sql);
if(mysqli_error($adsConn)){
echo mysqli_error($adsConn);
}else{
echo "OK";
}