PHP和数据访问之(插入。删除。和更新数据)

时间:2024-01-18 14:25:38

插入:

<?php
$conn=@new mysqli('localhost','root','123','mytestdb');
$q_str=<<<EOM
insert into users(name,email,date)values('xiaoxue','xue@163.com',now());
EOM;
$result=@$conn->query($q_str);
if($result===false){
echo "Connect Failed with:$conn->error<br/>\n";
$conn->close();
exit;
}else{
echo "Create new user with id:{$conn->insert_id}";
}
?>

可以查询mysqli类的insert_id属性

更新:

<?php
$conn=@new mysqli('localhost','root','123','mytestdb');
$q_str=<<<EOM
update users set name='jian' where id=7;
EOM;
$result=@$conn->query($q_str);
if($result===false){
echo "Connect Failed with:$conn->error<br/>\n";
$conn->close();
exit;
}else{
echo "I modified {$conn->affected_rows} rows.<br/>\n";
}
?>

 可以查询mysqli类的affected_rows属性。