MySQLi常用CURD操作
- 连接数据库:
mysqli(主机名,用户名,密码,数据库名)
- 数据库操作:
(1)stmt_init()
,返回预处理对象
(2)prepare(SQL语句)
,创建语句对象
(3)bind_param(参数类型,参数列表)
,绑定参数
(4)execite()
,执行
(5)affected_row()
,返回受影响的记录数
(6)insert_id
,新增的记录的ID
(7)error
,错误信息,字符串
(8)fetch(),获取一条记录,stmt方法
(9)get_result()
,返回结果集对象实例,结果集方式方法
(10)fetch_array()
,一条记录结果,为索引+关联的一维数组,结果集方式方法
(11)fetch_row()
,一条记录结果,为索引一维数组,结果集方式方法
(12)fetch_assoc()
,一条记录结果,为关联一维数组,结果集方式方法
(13)fetch_all
,一次性获取所有记录,索引二维数组,若要改成关联数组,需添加参数MYSQL_ASSOC
,结果集方式方法
(14)data_seek()
,数据库指针复位
(15)free_result()
,释放结果集
- 关闭数据库连接:
close()
使用mysqli操作数据库
数据表
新增操作
<?php
//连接数据库
$conn=new mysqli('localhost','root','root','test_db');
//准备sql语句
$sql="insert `user` set `name`=? , `age` = ?";
//预处理方式
$stmt=$conn->stmt_init();
//创建语句对象
$stmt->prepare($sql);
//绑定参数
$name='Jane';
$age=35;
$stmt->bind_param('si',$name,$age);
//执行
$stmt->execute();
//处理执行结果
if($stmt->affected_rows){
echo "新增成功,新增的ID是{$stmt->insert_id}";
}else{
echo "新增失败,错误原因{$stmt->error}";
}
//关闭数据库连接
$stmt->close();
修改操作
<?php
//连接数据库
$conn=new mysqli('localhost','root','root','test_db');
//准备sql语句
$sql="update `user` set `name`=? where id=?";
//预处理方式
$stmt=$conn->stmt_init();
//创建语句对象
$stmt->prepare($sql);
//绑定参数
$name='Maria';
$id=5;
$stmt->bind_param('si',$name,$id);
//执行
$stmt->execute();
//处理执行结果
if($stmt->affected_rows){
echo "修改成功";
}else{
echo "修改失败,错误原因{$stmt->error}";
}
//关闭数据库连接
$stmt->close();
删除操作
<?php
//连接数据库
$conn=new mysqli('localhost','root','root','test_db');
//准备sql语句
$sql="delete from `user` where id=?";
//预处理方式
$stmt=$conn->stmt_init();
//创建语句对象
$stmt->prepare($sql);
//绑定参数
$id=5;
$stmt->bind_param('i',$id);
//执行
$stmt->execute();
//处理执行结果
if($stmt->affected_rows){
echo "删除成功";
}else{
echo "删除失败,错误原因{$stmt->error}";
}
//关闭数据库连接
$stmt->close();
查询操作
<?php
//连接数据库
$conn=new mysqli('localhost','root','root','test_db');
//准备sql语句
$sql="select * from `user`";
//预处理方式
$stmt=$conn->stmt_init();
//创建语句对象
$stmt->prepare($sql);
//绑定参数
$stmt->bind_result($id,$name,$age);
//执行
$stmt->execute();
//处理执行结果
while($result=$stmt->fetch()){
echo "{$name}的年龄是{$age}<br/>";
}
//释放结果集
$stmt->free_result();
//关闭数据库连接
$stmt->close();
通过接口实现多态
数据操作接口
<?php
namespace db;
//定义常用参数接口
interface IParam{
const HOST='localhost';//主机名称
const USERNAME='root';//用户名
const PASSWORD='root';//密码
const PORT='3306';//端口号
const TYPE='mysql';//数据库类型
const CHARSET='utf8';//编码集
const DBNAME='test_db';//数据库名称
}
//定义构造方法接口,用于连接数据库
interface IStruction{
public function __construct(...$paramArr);
}
//定义常用数据库操作方法接口
interface IMethod extends IParam,IStruction{
public function insert(string $tableName,array $data);//添加
public function select(string $tableName,string $field,string $where);//查询
public function update(string $tableName,array $data,string $where);//修改
public function delete(string $tableName,string $where);//删除
}
pdo数据库操作类
<?php
namespace db;
require_once 'interface.php';
class Pdo_db implements IMethod{
private $pdo;
public function __construct(...$paramArr){
list($dsn, $username, $password) = $paramArr;
$this->pdo=new \PDO($dsn, $username, $password);
}
public function insert(string $tableName,array $data){
$param='';
foreach($data as $k=>$v){
if(gettype($v)=='integer'){
$param.=$k."= {$v},";
}else{
$param.=$k."= '{$v}',";
}
}
$param=rtrim($param,',');
$sql="insert `{$tableName}` set {$param} ;";
$stmt=$this->pdo->prepare($sql);
$stmt->execute();
if($stmt->rowCount()){
echo "新增成功,新增的记录ID为{$pdo->lastInsertId()}";
}else{
echo "新增失败,错误为:{$pdo->errorInfo()}";
}
}
public function select(string $tableName,string $field,string $where){
$sql="select {$field} from `{$tableName}` where {$where}";
$stmt=$this->pdo->prepare($sql);
$stmt->execute();
while($result=$stmt->fetch(\PDO::FETCH_ASSOC)){
var_dump($result);
echo '<br/>';
}
}
public function update(string $tableName,array $data,string $where){
$param='';
foreach($data as $k=>$v){
if(gettype($v)=='integer'){
$param.=$k."= {$v},";
}else{
$param.=$k."= '{$v}',";
}
}
$param=rtrim($param,',');
$sql="update `{$tableName}` set {$param} where {$where}";
$stmt=$this->pdo->prepare($sql);
$stmt->execute();
if($stmt->rowCount()){
echo "修改成功";
}else{
echo "修改失败,错误为:{$pdo->errorInfo()}";
}
}
public function delete(string $tableName,string $where){
$sql="delete from `{$tableName}` where {$where}";
$stmt=$this->pdo->prepare($sql);
$stmt->execute();
if($stmt->rowCount()){
echo "删除成功";
}else{
echo "删除失败,错误为:{$pdo->errorInfo()}";
}
}
}
mysqli数据库操作类
<?php
namespace db;
require_once 'interface.php';
class Mysqli_db implements IMethod{
private $conn;
public function __construct(...$paramArr){
$this->conn=new mysqli($paramArr['host'],$paramArr['username'],$paramArr['password'],$paramArr['dbname']);
}
public function insert(string $tableName,array $data){
$param='';
foreach($data as $k=>$v){
if(gettype($v)=='integer'){
$param.=$k."= {$v},";
}else{
$param.=$k."= '{$v}',";
}
}
$param=rtrim($param,',');
$sql="insert `{$tableName}` set {$param} ;";
$stmt=$this->conn->stmt_init();
$stmt->prepare($sql);
$stmt->execute();
if($stmt->affected_rows){
echo "新增成功,新增的ID是{$stmt->insert_id}";
}else{
echo "新增失败,错误原因{$stmt->error}";
}
$stmt->close();
}
public function select(string $tableName,string $field,string $where){
$sql="select {$field} from `{$tableName}` where {$where}";
$stmt=$this->conn->stmt_init();
$stmt->prepare($sql);
$stmt->execute();
while($result=$stmt->fetch_assoc()){
echo '<pre>'.print_r($result,1).'</pre>';
}
$stmt->free_result();
$stmt->close();
}
public function update(string $tableName,array $data,string $where){
$param='';
foreach($data as $k=>$v){
if(gettype($v)=='integer'){
$param.=$k."= {$v},";
}else{
$param.=$k."= '{$v}',";
}
}
$param=rtrim($param,',');
$sql="update `{$tableName}` set {$param} where {$where}";
$stmt=$this->conn->stmt_init();
$stmt->prepare($sql);
$stmt->execute();
if($stmt->affected_rows){
echo "修改成功";
}else{
echo "修改失败,错误为:{$pdo->errorInfo()}";
}
$stmt->close();
}
public function delete(string $tableName,string $where){
$sql="delete from `{$tableName}` where {$where}";
$stmt=$this->conn->stmt_init();
$stmt->prepare($sql);
$stmt->execute();
if($stmt->affected_rows){
echo "删除成功";
}else{
echo "删除失败,错误为:{$pdo->errorInfo()}";
}
$stmt->close();
}
}
测试
<?php
namespace db;
require_once 'pdo_db.php';
require_once 'mysqli_db.php';
$dsn=IParam::TYPE . ':host='.IParam::HOST . ';dbname=' . IParam::DBNAME . ';charset='.IParam::CHARSET;
$link=new Pdo_db($dsn, IParam::USERNAME, IParam::PASSWORD);
$link->select('user','*','id=1');
运行结果