作业一
mysqli相关知识:
作业二
mysqli常见的CURD操作
1增加数据:
1、代码:
<?php
namespace part1;
use mysqli;
//连接数据库
$mysql=new mysqli('NewYear.com','NY','123456','newyear.com');
$stmt=$mysql->stmt_init();
$sql="INSERT site_info SET 类型=?,优先级=?,标题=?,url=?,image=?,分类=?,标签=?;";
$data=['测试1',1,'测试标题测试标题1','http://newyear.com','image/001.jpg','无','测试1'];
list($type,$order,$site,$url,$image,$class,$tag)=$data;
$stmt->prepare($sql);
$stmt->bind_param('sisssss',$type,$order,$site,$url,$image,$class,$tag);
$stmt->execute();
if ($stmt->affected_rows===1):
echo '添加记录成功,新的主键ID:'.$stmt->insert_id;
else:
echo '添加失败'.$stmt->error;
endif;
$stmt->close();
2、效果图
2、更新数据:
跟新和怎加基本一致只是SQL语句不同,另外mysqli_stmt类中insert_id();在更新下无法使用。
1、核心代码:
$stmt=$mysql->stmt_init();
$sql="UPDATE `site_info` SET `标题`=? WHERE id=? ";
$stmt->prepare($sql);
$data=['更新测试',62];
list($site,$id)=$data;
$stmt->bind_param('si',$site,$id);
2、效果图
3、删除数据
1、核心代码:
$sql='DELETE FROM `site_info` WHERE `id`=?';
$stmt->prepare($sql);
$id=61;
$stmt->bind_param('i',$id);
2、效果图
4、查询操作
1、用mysqli_stmt类查询:
(1)代码:
<?php
namespace part1;
use mysqli;
$mysql=new mysqli('NewYear.com','NY','123456','newyear.com');
$stmt=$mysql->stmt_init();
$sql='SELECT `id`,`类型`,`优先级`,`标题`,`url`,`image`,`分类`,`标签` FROM `site_info` WHERE `id`>=?';
$stmt->prepare($sql);
$id=59;
$stmt->bind_param('i',$id);
$stmt->execute();
$stmt->bind_result($id,$type,$order,$site,$url,$image,$class,$tag);
//$stmt->fetch();
while($stmt->fetch()):
echo $id,$type,$order,$site,$url,$image,$class,$tag.'<br>';
endwhile;
$stmt->free_result();
$stmt->close();
(2)、效果图:
2、用mysqli_result类查询:
(1)、代码:
<?php
namespace part1;
use mysqli;
//连接数据库
$mysql=new mysqli('NewYear.com','NY','123456','newyear.com');
//mysqli_stmt类初始化
$stmt=$mysql->stmt_init();
//准备sql语句
$sql='SELECT `id`,`类型`,`优先级`,`标题`,`url`,`image`,`分类`,`标签` FROM `site_info` WHERE `id`>?';
//预处理
$stmt->prepare($sql);
$id=57;
//绑定数据
$stmt->bind_param('i',$id);
//执行
$stmt->execute();
//获取结果返回mysql_result类实例
$result=$stmt->get_result();
//获取数据
//$log=$result->fetch_array();
//$log=$result->fetch_row();
//$log=$result->fetch_assoc();
$logs=$result->fetch_all(MYSQLI_ASSOC);
//处理输出数据
foreach ($logs as $log):
echo '<pre>'.print_r($log,true).'</pre>';
endforeach;
//echo print_r($log,true);
//$stmt->bind_result($id,$type,$order,$site,$url,$image,$class,$tag);
//while($stmt->fetch()):
// echo $id,$type,$order,$site,$url,$image,$class,$tag.'<br>';
//endwhile;
//释放结果集
$result->free_result();
//关闭数据库
$stmt->close();
(2)、效果图
作业三
1、类接口:
<?php
namespace part1;
//数据库连接常量
interface iDbParam
{
const HOST='NewYear.com';
const TYPE='mysql';
const DBNAME='newyear.com';
const USER_NAME='root';
const PASSWORD='root';
const CHARSET='utf8';
const PORT='3306';
}
//构造接口方法
interface iDbLink
{
public function __construct(...$linkParams);
}
interface iCURD extends iDbParam,iDbLink
{
public function insert(array $data);
public function update(array $data, string $where);
public function delete(string $where);
public function select(string $where);
}
(2)PDO方法代码:
<?php
namespace part1;
use PDO;
require '0211-1.php';
class Db_PDO implements iCURD
{
private $pdo=null;
public function __construct(...$linkParams)
{
list($dsn,$username,$password)=$linkParams;
$this->pdo=new PDO($dsn,$username,$password);
}
public function insert(array $data)
{
$sql='INSERT site_info SET 类型=?,优先级=?,标题=?,url=?,image=?,分类=?,标签=?;';
$stmt=$this->pdo->prepare($sql);
$stmt->execute($data);
return $stmt->rowCount()===1 ? '新增成功' : '新增失败';
}
public function update(array $data, string $where)
{
$params='';
foreach ($data as $key=>$value){
$params.="`{$key}`='{$value}',";
}
$params=rtrim($params,',');
$sql="UPDATE `site_info` SET {$params} WHERE {$where}";
// die($sql);
$stmt=$this->pdo->prepare($sql);
// die($stmt);
$stmt->execute();
return $stmt->rowCount()===1 ? '更新成功' : '更新失败';
}
public function delete(string $where)
{
$sql="DELETE FROM `site_info` WHERE {$where}";
// die($sql);
$stmt=$this->pdo->prepare($sql);
$stmt->execute();
return $stmt->rowCount()===1 ? '删除成功' : '删除失败';
}
public function select(string $where)
{
$where= empty($where) ? $where : 'WHERE '.$where;
$sql="SELECT * FROM `site_info` {$where}";
// die($sql);
$stmt = $this->pdo->prepare($sql);
// die($stmt);
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
}
(3)MySQLi方法代码:
<?php
namespace part1;
require '0211-1.php';
use mysqli;
class MySQL implements iCURD
{
private $mysqli = null;
public function __construct(...$linkParams)
{
list($host,$username,$password,$dbname)=$linkParams;
$this->mysqli=new mysqli($host,$username,$password,$dbname);
$this->mysqli->set_charset('utf8');
}
public function insert(array $data){
$sql='INSERT site_info SET 类型=?,优先级=?,标题=?,url=?,image=?,分类=?,标签=?;';
$stmt=$this->mysqli->stmt_init();
$stmt->prepare($sql);
list($type,$order,$site,$url,$image,$class,$tag)=$data;
$stmt->bind_param('sisssss',$type,$order,$site,$url,$image,$class,$tag);
$stmt->execute();
return $stmt->affected_rows===1 ? '增加成功' : '增加失败';
}
public function update(array $data, string $where)
{
$params='';
foreach ($data as $key=>$value){
$params.="`{$key}`='{$value}',";
}
$params=rtrim($params,',');
$sql="UPDATE `site_info` SET {$params} WHERE {$where}";
// die($sql);
$stmt=$this->mysqli->stmt_init();
$stmt->prepare($sql);
$stmt->execute();
return $stmt->affected_rows===1 ? '更新成功' : '更新失败';
}
public function delete(string $where)
{
$sql="DELETE FROM `site_info` WHERE {$where}";
$stmt=$this->mysqli->stmt_init();
$stmt->prepare($sql);
$stmt->execute();
return $stmt->affected_rows===1 ? '删除成功' : '删除失败';
}
public function select(string $where)
{
$where= empty($where) ? $where : 'WHERE '.$where;
$sql="SELECT * FROM `site_info`".$where;
$stmt=$this->mysqli->stmt_init();
$stmt->prepare($sql);
$stmt->execute();
return $stmt->get_result();
}
}
(4)通用类:数据库操作:
<?php
namespace part1;
use part1\iCURD;
class DB
{
public static function insert(iCURD $db, array $data)
{
return $db->insert($data);
}
public static function update(iCURD $db, array $data,string $where)
{
return $db->update($data,$where);
}
public static function delete(iCURD $db,string $where)
{
return $db->delete($where);
}
public static function select(iCURD $db,string $where)
{
return $db->select($where);
}
}
(5)操作代码:
a、pdo操作数据库:
<?php
//PDO操作数据库
namespace part1;
require '0211-2.php';
require '0211-3.php';
$dsn=$dsn = iDbParam::TYPE . ':host='.'NewYear.com'. ';dbname='.'newyear.com' . ';charset='.iDbParam::CHARSET;
$link=new Db_PDO($dsn,'NY','123456');
//echo DB::insert($link,['测试0211',1,'测试标题测试标题0211','http://newyear.com','image/001.jpg','无','测试1']);
//echo DB::update($link,['类型'=>'PDO测试'],'`id`=62');
//echo DB::delete($link,'id=64');
foreach (DB::select($link,'id>54') as $arr)
{
echo '<pre>'.print_r($arr,true).'</pre>>';
}
b、mysqli操作数据库:
<?php
namespace part1;
require '0211-21.php';
require '0211-3.php';
$link = new MySQL('NewYear.com', 'NY', '123456','newyear.com');
//echo DB::insert($link,['测试mysqli',1,'测试标题测试标题0211','http://newyear.com','image/001.jpg','无','测试1']);
//echo DB::update($link,['类型'=>'PDO-测试'],'`id`=62');
//echo DB::delete($link,'id=63');
$result=DB::select($link,'id=59');
$results=$result->fetch_all(MYSQLI_ASSOC);
echo '<pre>'.print_r($results,true).'</pre>';
四:补充
mysql_result:
$result->num_rows:返回结果的记录行数;