[MySqli扩展]③--插入、更新、删除记录

$mysqli->query

$mysqli->affected_rows

$mysqli->insert_id

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 . '位用户
'; echo "有" . $mysqli->affected_rows . "记录被影响
"; } else { echo 'ERROR ' . $mysqli->errno . ":" . $mysqli->error; } ?>
Paste_Image.png
[MySqli扩展]③--插入、更新、删除记录_第1张图片
Paste_Image.png
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;
}

?>
Paste_Image.png
[MySqli扩展]③--插入、更新、删除记录_第2张图片
Paste_Image.png

affect_rows值为3种
1.受影响的记录条数
2.-1,代表SAL语句有问题
3.0 代表没有受影响记录的条数

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 . "条记录被删除
"; } else { echo "ERROR " . $mysqli->errno . ":" . $mysqli->error; } //关闭到MYSQL的a连接 $mysqli->close(); ?>
Paste_Image.png
[MySqli扩展]③--插入、更新、删除记录_第3张图片
Paste_Image.png

你可能感兴趣的:([MySqli扩展]③--插入、更新、删除记录)