-
php有三种方式来操作mysql数据库
- mysql扩展库
- mysqli扩展库
- pdo
-
mysql(mysqli)扩展库和数据库的区别
-
mysql数据库用于存放数据
-
mysql数据库的三层结构示意图
-
mysql(mysqli)扩展库是函数集合,用于操作数据库。
-
mysql扩展库和mysqli扩展库的比较
- mysqli(mysql improve)是mysql扩展库的增强版。
- mysqli扩展库的安全性和稳定性有所提高。
- mysqli扩展库支持面向过程和面向对象的编程风格(面向过程的函数库和面向对象的函数库),而mysql扩展库只支持面向过程的编程风格。
-
简单案例
-
搭建环境
- 查看php现支持哪些扩展库:使用语句<?php phpinfo() ?>
- 若未启动mysql(mysqli)数据库,在php.ini中去配置mysql扩展库
-
创建用户表
- 登录数据库(设置环境变量→cmd命令窗口→mysql -h主机名 -u用户名 -p→回车→输入密码),出现红框中的现象是密码输入错误。
- 操作
- 登录数据库(设置环境变量→cmd命令窗口→mysql -h主机名 -u用户名 -p→回车→输入密码),出现红框中的现象是密码输入错误。
显示数据库
mysql> show databases;
想要创建新的数据库
mysql> create database test;
使用数据库
mysql> use test;
查看数据库下的表
mysql> show tables;
创建表(此处复制粘贴,ENGINE=InnoDB DEFAULT CHARSET=utf8表示编码格式)
出现ERROR 1064(42000)是语法错误(括号不对,少分号...)
CREATE TABLE user1(
id int not null auto_increment,
name varchar(32) not null,
age int not null,
password varchar(64) not null,
primary key (id)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
插入数据(md5函数是加密函数,对于密码是保密的)
mysql> insert into user1 values(1,'li',15,md5('123'));
查看数据
mysql> select * from user1;
- php程序:使用mysqli扩展库操作mysql(注意查询mysql扩展库和mysqli扩展库的区别)
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
</head>
<body>
<?php
//与mysqli连接
$conn=new mysqli("localhost","root","********");
//选择数据库
$conn->select_db('test');
//设置操作编码
$conn->query("set names utf-8");
//语句
$sql="select * from user1";
//发送一条sql语句,返回资源
$res=$conn->query($sql);
//var_dump($res);
//显示数据
//$res->fetch_row()取一行数据,返回索引数组
while($row=$res->fetch_row()){
//$row是一个数组
//var_dump($row);
//echo "<br/> $row[0]--$row[1]--$row[2]--$row[3]";
foreach($row as $key=>$value){
echo $value." ";
}
echo "<br/>";
}
//$res->fetch_assoc()取一行数据,返回关联数组
/*
while($row=$res->fetch_assoc()){
//$row是一个数组
//var_dump($row);
echo "<br/>".$row['id']."--".$row['name']."--".$row['age']."--".$row['password'];
}
*/
//$res->fetch_array()取一行数据,返回索引数组和关联数组(两种)
/*
while($row=$res->fetch_array()){
//$row是一个数组
var_dump($row);
}
*/
//$res->fetch_object()取一行数据,当做一个对象来返回
/*
while($row=$res->fetch_object()){
//$row是一个数组
//var_dump($row);
echo "<br/>".$row->id."--".$row->name."--".$row->age."--".$row->password;
}
*/
//释放内存(注意!!!)
mysqli_free_result($res);
//关闭连接(注意!!!)
mysqli_close($conn);
?>
</body>
</html>
- 四种方式获取表的行数据
//$res->fetch_row()取一行数据,返回索引数组
$row=$res->fetch_row();
//$res->fetch_assoc()取一行数据,返回关联数组
$row=$res->fetch_assoc();
//$res->fetch_array()取一行数据,返回索引数组和关联数组(两种)
$row=$res->fetch_array();
//$res->fetch_object()取一行数据,当做一个对象来返回
$row=$res->fetch_object();
- 程序运行完毕后,需要释放与数据库进行交互的资源:数据库连接($conn)和查询结果($res)。数据库连接($conn)使用完毕后要及时释放。
- 插入数据
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
</head>
<body>
<?php
$conn=new mysqli("localhost","root","********");
$conn->select_db("test");
$conn->query("set names utf-8");
$sql="insert into user1 values(5,'tan',20,md5('189'))";
$res=$conn->query($sql);//返回bool
//var_dump($res);
if($res){
echo "insert success";
}else{
echo "insert false";
}
mysqli_close($conn);
?>
</body>
</html>
- 删除数据
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
</head>
<body>
<?php
$conn=new mysqli("localhost","root","********");
$conn->select_db("test");
$conn->query("set names utf-8");
$sql="delete from user1 where id=5";
$res=$conn->query($sql);
//var_dump($res); //返回bool
//数据行数(看有没有该行)
if(mysqli_affected_rows($conn)>0){
echo "success";
}else{
echo "没有此id";
}
mysqli_close($conn);
?>
</body>
</html>
- 更新数据
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
</head>
<body>
<?php
$conn=new mysqli("localhost","root","********");
$conn->select_db("test");
$conn->query("set names utf-8");
$sql="update user1 set age=88 where id=2";
$res=$conn->query($sql);
//var_dump($res); //返回bool
if($res){
echo "update success";
}else{
echo "update false";
}
mysqli_close($conn);
?>
</body>
</html>
封装sqlTool
- 之前的使用方法代码的复用性和可维护性低,将其封装成一个类。
//sqlToolDefine.class.php
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
</head>
<body>
<?php
class sqlToolDefine{
private $conn;
private $host="localhost";
private $user="root";
private $password="********";
function __construct($db){
$this->conn=new mysqli($this->host,$this->user,$this->password);
$this->conn->select_db($db);
$this->conn->query("set names utf-8");
}
//select
public function execute_dql($sql){
$res=$this->conn->query($sql);
while($row=$res->fetch_row()){
//$row是一个数组
//var_dump($row);
//echo "<br/> $row[0]--$row[1]--$row[2]--$row[3]";
foreach($row as $key=>$value){
echo $value." ";
}
echo "<br/>";
}
mysqli_free_result($res);
}
//update,delete,insert
public function execute_dml($sql){
$res=$this->conn->query($sql);
if(!$res){
return 0; //失败
}else{
if(mysqli_affected_rows($this->conn)>0){
return 1; //表示成功
}else{
return 2; //表示没有此行
}
}
}
public function close(){
mysqli_close($this->conn);
}
}
?>
</body>
</html>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
</head>
<body>
<?php
require_once "sqlToolDefine.class.php";
$sqltool=new sqlToolDefine("test");
$sql="insert into user1 values(5,'tan',20,md5('189'))";
$res=$sqltool->execute_dml($sql);
switch($res){
case 0: echo "false";
break;
case 1: echo "success";
break;
case 2: echo "no row";
break;
}
$sql="delete from user1 where id=5";
$res=$sqltool->execute_dml($sql);
switch($res){
case 0: echo "false";
break;
case 1: echo "success";
break;
case 2: echo "no row";
break;
}
$sql="select * from user1";
$sqltool->execute_dql($sql);
$sqltool->close();
?>
</body>
</html>