什么是类,什么是对象?
类是对象模板,通过实例化类得到的就是对象,列如:汽车属于类,宝马,奔驰,本田等等是就是一个个的对象.
2.
实例
<?php /** * */ class GirlFriend3 { //访问控制:private private $name; //年龄 private $age; //三维 private $stature = []; //属性收集器 private $data=[]; //声明构造方法:在类实例化的时候,自动调用 public function __construct($name,$age,array $stature) { //private 访问符限制的属性仅在当前对象内可以使用 $this->name = $name; $this->age = $age; $this->stature = $stature; } //创建对外访问的公共接口 //类中用双下划线的方法是系统定义的,有系统自动调用,叫魔术方法 public function __get($name) { //查找传进来的参数对应的属性 $msg=null;//先设置$msg为null if(isset($this->name)){ $msg=$this->name;//如果属性存在,则赋值给$msg }elseif(isset($this->data[$name])){ $msg=$this->data[$name];//如果不存在,查找属性收集器里面是否存在,存在则赋值给$msg }else{ $msg='无此属性';//不存在,返回无此属性 } return $msg; } //设置器 public function __set($name,$value) { //把传进来的参数设置为类的对应属性值 $this->name = $value; } }
运行实例 »
点击 "运行实例" 按钮查看在线实例
实例
<?php require 'class/GirlFriend3.php'; $girlfriend3=new GirlFriend3('波多野结衣',32,[80,80,80]); echo $girlfriend3->name,"<br>"; echo $girlfriend3->age,"<br>"; $girlfriend3->age=35; echo $girlfriend3->age,"<br>"; $girlfriend3->name='苍井空'; echo $girlfriend3->name,"<br>"; $girlfriend3->stature=[86,80,90]; echo $girlfriend3->stature,"<br>";
运行实例 »
点击 "运行实例" 按钮查看在线实例
----------------------------------------------------------------
3.MySQL常用的增删改查语句
实例
<?php //插入: $SQL="INSERT INTO `staff` (`name`,`salary`) VALUES ('zhangsan','8000');"; $SQL="INSERT INTO `staff` (`name`,`salary`) VALUES ('lisi','5000');"; $SQL="INSERT INTO `staff` (`name`,`salary`) VALUES ('wangwu','7100');"; $SQL="INSERT INTO `staff` (`name`,`sex`,`salary`) VALUES ('zhaoliu','1','8000');"; $SQL="INSERT INTO `staff` (`name`,`salary`) VALUES ('zhangsan','8000');"; $SQL="INSERT INTO `staff` (`name`,`salary`) VALUES ('zhangsan','8000');"; $SQL="INSERT `staff` (`name`,`salary`) VALUES ('zhangsan','8000');"; //更新 $SQL="UPDATE `staff` SET `salary`=8500 WHERE `id`=10;"; $SQL="UPDATE `staff` SET `salary`=7000,`sex`=0 WHERE `id`=8;"; $SQL="UPDATE `staff` SET `salary`=5000 WHERE `id`=15;"; $SQL="UPDATE `staff` SET `salary`=8500 WHERE `id`=10;"; $SQL="UPDATE `staff` SET `salary`=8500 WHERE `salary`=8000;"; //查询 $SQL= "SELECT * FROM `staff` ;"; $SQL= "SELECT `id`,`name`,`salary` FROM `staff` ;"; $SQL= "SELECT count(*) FROM `staff` ;"; $SQL= "SELECT * FROM `staff` WHERE `id`>10;"; $SQL= "SELECT * FROM `staff` WHERE `salary`<9000;;"; $SQL= "SELECT * FROM `staff` WHERE `id`>10 && `salary`>3000;"; $SQL= "SELECT * FROM `staff` ;"; $SQL= "SELECT 20+30 AS PLUS"; $SQL= "SELECT * FROM `staff` ;"; //删除 $SQL= "DELETE FROM `staff` WHERE `id`=10;"; $SQL= "DELETE FROM `staff` WHERE `salary`=8000;"; $SQL= "DELETE FROM `staff` WHERE `id`=14;"; $SQL= "DELETE FROM `staff` WHERE `name`='zhangsan';"; $SQL= "DELETE FROM `staff` WHERE `id`=12;"; $SQL= "DELETE FROM `staff` WHERE `id`=1;";
运行实例 »
点击 "运行实例" 按钮查看在线实例
4数据库的连接与检测
实例
<?php $db = [ 'host' => '127.0.0.1', 'user' => 'root', 'pass' => 'root', 'name' => 'php', 'charset' => 'utf8', ];
运行实例 »
点击 "运行实例" 按钮查看在线实例
实例
<?php /** * 数据库的连接 */ //创建一个数据库连接,并返回mysqli对象 require 'config.php'; error_reporting(E_ALL ^E_WARNING);//设置错误提示等级; //简化: 将连接参数转为变量或数组 $mysqli = new mysqli($db['host'],$db['user'],$db['pass'],$db['name']); //判断是否连接成功? if ($mysqli->connect_errno) { // 自定义错误提示信息 die('连接错误'.$mysqli->connect_errno.': '. $mysqli->connect_error); } echo '<h1>连接成功</h1>'; // 设置默认数据库 //$mysqli->select_db($db_name); //设置客 户端默认的字符编码集 $mysqli->set_charset($db['charset']); // 将默认数据库在连接的时候,直接通过构造方法传入
运行实例 »
点击 "运行实例" 按钮查看在线实例