$mysqli->query
$mysqli->affected_rows
$mysqli->insert_id
<?php
$mysqli = @new mysqli('localhost', 'root', '', 'test');
if ($mysqli->connect_errno) {
die('CONNECT ERROR: ' . $mysqli->connect_error);
}
$mysqli->set_charset('utf8');
$sql = "INSERT user(username,password) VALUES('king','king'),('queen','queen');";
$res = $mysqli->query($sql);
if ($res) {
echo "恭喜您注册成功,您是网站第" . $mysqli->insert_id . '位用户<br/>';
echo "有" . $mysqli->affected_rows . "记录被影响<br/>";
} else {
echo 'ERROR ' . $mysqli->errno . ":" . $mysqli->error;
}
?>
<?php
$mysqli = @new mysqli('localhost', 'root', '', 'test');
if ($mysqli->connect_errno) {
die('CONNECT ERROR: ' . $mysqli->connect_error);
}
$mysqli->set_charset('utf8');
$sql = "UPDATE user SET age=age+10";
$res = $mysqli->query($sql);
if ($res) {
echo $mysqli->affected_rows . "条记录被更新";
} else {
echo "ERROR" . $mysqli->errno . ":" . $mysqli->error;
}
?>
affect_rows值为3种
1.受影响的记录条数
2.-1,代表SAL语句有问题
3.0 代表没有受影响记录的条数
<?php
$mysqli = @new mysqli('localhost', 'root', '', 'test');
if ($mysqli->connect_errno) {
die('CONNECT ERROR: ' . $mysqli->connect_error);
}
$mysqli->set_charset('utf8');
$sql = "DELETE FROM user WHERE id>=6";
$res = $mysqli->query($sql);
if ($res) {
echo $mysqli->affected_rows . "条记录被删除<br/>";
} else {
echo "ERROR " . $mysqli->errno . ":" . $mysqli->error;
}
//关闭到MYSQL的a连接
$mysqli->close();
?>