什么类,什么是对象
php讲师是一个类,Peter朱老师就是这个类一个对象
篮球运动员是类,表示会打篮球的的人,这样人人有很多,姚明是篮球运动员的一个实例
自定义类与实例化
实例
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2018/8/31 * Time: 14:29 */ header('content-type:text/html;charset=utf-8'); class Player{ //private 私有属性 private $name; private $age; private $height; //构造方法,创建实例时自动调用,初始化对象的属性 public function __construct($name,$age,$height) { $this->name=$name; $this->age=$age; $this->height=$height; } //创建对外访问的公共接口 public function __get($name) { return $this->$name; } public function __set($name, $value) { $this->$name = $value; } }
运行实例 »
点击 "运行实例" 按钮查看在线实例
实例
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2018/8/31 * Time: 14:44 */ require 'class/Player.php'; $yaoming = new Player('姚明','38','226'); echo '姓名:',$yaoming->name,'<br>'; echo '年龄:',$yaoming->age,'<br>'; echo '身高:',$yaoming->height,'<br>';
运行实例 »
点击 "运行实例" 按钮查看在线实例
MySQL常用的增删改查语句(CURD)
实例
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2018/9/4 * Time: 21:48 */ //加载连接数据库文件 require 'connect.php'; /*//准备sql语句 $sql = "INSERT INTO `player` SET `name`=?,`salary`=?;"; //创建sql预处理对象 $stmt = $db_con->prepare($sql); //绑定参数 //$name = 'davis'; //$salary = 3000; //$stmt->bind_param('si',$name,$salary); //执行sql语句 if($stmt->execute()){ //执行成功 //检测是否有数据新增 if($stmt->affected_rows > 0){ echo '新增了'.$stmt->affected_rows.'条数据,新增记录的主键id是:'.$stmt->insert_id,'<br>'; }else{ echo '没有新增记录'; } }else{ exit($stmt->errno.':'.$stmt->error); } */ //准备sql语句 $sql = "INSERT INTO `player` SET `name`=?,`salary`=?;"; //创建sql预处理对象 $stmt = $db_con->prepare($sql); //用数组添加多条数据 $data[] = ['name'=>'海沃德','salary'=>2970]; $data[] = ['name'=>'米尔萨普','salary'=>3000]; $data[] = ['name'=>'德罗赞','salary'=>2774]; //绑定参数 $stmt->bind_param('si',$name,$salary); //循环执行 foreach ($data as $player){ $name = $player['name']; $salary = $player['salary']; //执行sql语句 if($stmt->execute()){ //执行成功 //检测是否有数据新增 if($stmt->affected_rows > 0){ echo '新增了'.$stmt->affected_rows.'条数据,新增记录的主键id是:'.$stmt->insert_id,'<br>'; }else{ echo '没有新增记录'; } }else{ exit($stmt->errno.':'.$stmt->error); } } //注销stmt对象 $stmt->close(); //关闭连接 $db_con->close();
运行实例 »
点击 "运行实例" 按钮查看在线实例
实例
<?php //加载连接数据库文件 require 'connect.php'; //准备sql语句 $sql = "DELETE FROM `player` WHERE `id`=?;"; //创建sql预处理对象 $stmt = $db_con->prepare($sql); //绑定参数 $id = 16; $stmt->bind_param('i',$id); //执行sql语句 if($stmt->execute()){ //执行成功 //检测是否有数据新增 if($stmt->affected_rows > 0){ echo '删除了'.$stmt->affected_rows.'条数据<br>'; }else{ echo '没有删除记录'; } }else{ exit($stmt->errno.':'.$stmt->error); } //注销stmt对象 $stmt->close(); //关闭连接 $db_con->close();
运行实例 »
点击 "运行实例" 按钮查看在线实例
实例
<?php //加载连接数据库文件 require 'connect.php'; //准备sql语句 $sql = "UPDATE `player` SET `age`=? WHERE `id`=?;"; //创建sql预处理对象 $stmt = $db_con->prepare($sql); //绑定参数 $id = 15; $age = 32; $stmt->bind_param('ii',$age,$id); //执行sql语句 if($stmt->execute()){ //执行成功 //检测是否有数据新增 if($stmt->affected_rows > 0){ echo '更新了'.$stmt->affected_rows.'条数据,新增记录的主键id是:'.$stmt->insert_id,'<br>'; }else{ echo '没有更新记录'; } }else{ exit($stmt->errno.':'.$stmt->error); } //注销stmt对象 $stmt->close(); //关闭连接 $db_con->close();
运行实例 »
点击 "运行实例" 按钮查看在线实例
实例
<?php //加载连接数据库文件 require 'connect.php'; //准备sql语句 $sql = "SELECT `id`,`name` FROM `player` WHERE `salary` > ?;"; //创建sql预处理对象 $stmt = $db_con->stmt_init(); if($stmt->prepare($sql)){ //绑定参数 $salary = 3000; $stmt->bind_param('i',$salary); if($stmt->execute()){ //获取结果集 $stmt->store_result(); //将结果集的列绑定到变量上 $stmt->bind_result($id,$name); //判断结果接是否为空,不为空进行遍历 if($stmt->num_rows > 0){ //循环遍历结果集 //fetch每次获取一条记录并指针下移 while($stmt->fetch()){ echo 'id:'.$id.'---姓名:'.$name,'<br>'; } }else{ exit('没有数据'); } //释放结果集 $stmt->free_result(); }else{ exit($stmt->error); } }else{ exit($stmt->error); } //注销stmt对象 $stmt->close(); //关闭连接 $db_con->close();
运行实例 »
点击 "运行实例" 按钮查看在线实例
数据库的连接与检测
实例
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2018/9/4 * Time: 21:29 */ $db_host = '127.0.0.1'; $db_user = 'root'; $db_pass = 'root'; $db_name = 'php'; $db_charset = 'utf8';
运行实例 »
点击 "运行实例" 按钮查看在线实例
实例
<?php /** * 连接数据库 */ //加载配置文件 header('content-type:text/html;charset=utf-8'); require 'config.php'; //创建一个数据库连接对象 $db_con = new mysqli($db_host,$db_user,$db_pass,$db_name); //判断是否连接成功 if($db_con->connect_errno){ die('FAILED:'.$db_con->connect_error); } echo 'SUCCESS<br>'; //设置字符编码集 $db_con->set_charset($db_charset);
运行实例 »
点击 "运行实例" 按钮查看在线实例