<?php$dbhost="localhost";$dbuser="root";$dbpass="root";$dbname="member";$mysqli=newmysqli($dbhost,$dbuser,$dbpass,$dbname);if($mysqli->connect_errno){printf("Connect DataBase Fail,%s<br>",$mysqli->connect_error);exit();}echo"Connect DataBase Success<br>";$sql="INSERT INTO user(id,username,password) VALUES(null,'zs', '123')";if($mysqli->query($sql)){printf("Insert Data SuccessFully");}if($mysqli->errno){printf("Insert data Fail,%s<br>",$mysqli->error);}$mysqli->close();?>
7 删除操作
<?php$dbhost="localhost";$dbuser="root";$dbpass="root";$dbname="member";$mysqli=newmysqli($dbhost,$dbuser,$dbpass,$dbname);if($mysqli->connect_errno){printf("Connect database fail,%s<br>",$mysqli->connect_error);exit();}echo"Connect database success.<br>";$delete_sql='delete from user where id = 4';if($mysqli->query($delete_sql)){printf("delete success<br>");}if($mysqli->errno){printf("delete fail,%s<br>",$mysqli->error);}$mysqli->close();?>
8 修改操作
<?php$dbhost="localhost";$dbuser="root";$dbpass="root";$dbname="member";$mysqli=newmysqli($dbhost,$dbuser,$dbpass,$dbname);if($mysqli->connect_errno){printf("Connect database fail,%s<br>",$mysqli->connect_error);exit();}echo"Connect DataBase SuccessFully<br>";$update_sql='update user set author="john" where title = "jsp"';if($mysqli->query($update_sql)){echo"Update SuccessFully<br>";}if($mysqli->errno){printf("Update Fail %s<br>",$mysql->error);}$mysqli->close();?>
9 查询操作
<?php$dbhost="localhost";$dbuser="root";$dbpass="root";$dbname="member";$mysqli=newmysqli($dbhost,$dbuser,$dbpass,$dbname);if($mysqli->connect_errno){printf("Connection Database Fail,%s<br>",$mysqli->connect_error);exit();}echo"connect Database Successfully<br>";// $sql = 'SELECT title,author FROM user'; // 查询指定字段// $sql = 'SELECT * FROM user'; // 查询全部字段$sql='SELECT * FROM tbl WHERE author = "me"';// 条件查询$res=$mysqli->query($sql);// 返回一个结果集if($res->num_rows>0){// 判断记录数是否大于0while($row=$res->fetch_assoc()){printf("id:%s,title:%s, author:%s, date:%s<br>",$row['id'],$row['title'],$row['author'],$row['date']);// printf("title:%s, author:%s<br>",// $row['title'],$row['author']// );}}else{echo"No record found";}mysqli_free_result($res);// 释放结果集$mysqli->close();?>
10 Like查询
<?php$dbhost="localhost";$dbuser="root";$dbpass="root";$dbname="member";$mysqli=newmysqli($dbhost,$dbuser,$dbpass,$dbname);if($mysqli->connect_errno){printf("connect database fail,%s<br>",$mysqli->error);}echo"connect database success<br>";$sql='select * from user where title like "p%"';$res=$mysqli->query($sql);// 返回一个结果集if($res->num_rows>0){while($row=$res->fetch_assoc()){printf("id:%s id:%s id:%s id:%s<br>",$row['id'],$row['author'],$row['title'],$row['date']);}}else{printf("no record found<br>");}mysqli_free_result($res);// 释放结果集$mysqli->close();?>