作业1:1. 问答: 什么类,什么是对象,举例说明;
类与对象:
* 类是对象的母版,对象是类的实例
//类是一类事物的抽象概括,比如:动物类,他们都皮毛,四肢等,实例化后称为对象,比如猫,就是动物的一个实例了。
// 类由属性(成员变量)构成,属性也有相应的驱动方法(成员函数)
//下面是个汽车类 有成员和方法构成
class Cars{
public $wheelNum = 4;
public $colour = 'red';
public $fuelTank = '80L';
public function run (){
echo '踩油门,冲!'
}
public function getCol($col){
return $this->colour;
}
public function setCol($col){
echo $this->colour=$col;
}
}
// 下面是个cars的一个对象实例
$audi = new Cars();
echo $audi->colour; //访问它固有属性
echo $audi->run(); //调用方法
作业2:参考object/demo3.php,自定义类与实例化,要求必须将属性私有化,通过公共接口__set()和get()进行访问
实例
<?php /** 用构造函数来初始化成员变量; * 用魔术方法统一获取和更改私用属性; */ class webSite{ private $title; private $keyWords; private $url; //构造方法 public function __construct($title,$keyWords,$url) { $this->title=$title; $this->keyWords=$keyWords; $this->url=$url; } //创建对外的访问接口/方法 public function __get($name) //调用的时候,可以以下传三个参数吗?或者更多。 { return isset($this->$name)?$this->$name:null; } public function __set($name, $value) { $this->$name = $value; } } //用构造方法直接初始化对象 $website = new webSite('php中文网','教程,视频,手册','php.cn'); echo $website->title,'<br>'; echo $website->keyWords,'<br>'; echo $website->url,'<br>'; $website->title = 'php中文网学习站点'; //直接调用set更改。 echo $website->title,'<br>';
运行实例 »
点击 "运行实例" 按钮查看在线实例
作业3:MySQL常用的增删改查语句(CURD),每个语句必须写10遍以上;
实例
/*常用的数据库语句,围绕建立一个学生表*/ create database stu character set=utf8; /*drop database stu;*/ create table student(id int primary key auto_increment, name varchar(20) not null, sex varchar(10), birthdat date, class varchar(20), bursary int(10), address varchar(100)) auto_increment=100; /*表的修改*/ /*drop table student;*/ /*alter table student add phoneNum varchar(30); alter table student modify sex varchar(6); alter table student drop sex; rename table student to student1; alter table student1 character set=utf8; alter table student1 change id idno varchar(20);*/ /*表操作*/ /*插入了6调数据*/ insert into student (name,sex,birthdat,class,bursary) values('jimmy','male','19951212','3A',1500); insert into student (name,sex,class,bursary,address) values ('peter','male','3A',2000,'hefei'),('core','female','2A',1000,'zhuhai'); insert into student (name,sex,class,bursary) values ('amy','female','3A',500); insert into student (name,sex,birthdat,class,bursary,address) values ('zola','male','19961212','2A',0,'北京'),('len','male','19981212','1A',800,'上海'); /*更新语句*/ update student set bursary=100 where name='zola'; update student set address = '西藏',bursary= bursary+200 where name = 'jimmy'; /*删除语句*/ delete from student where bursary<1000; delete from student ; /*查询语句*/ select * from student; select name bursary from student where class='3A'; select name from student where class='3A' and bursary>1000; select * from student order by bursary desc; /*降序排列*/ select count(id) from student; select count(bursary) from student where bursary>500; select count(bursary) from student ; select avg(bursary) from student;
运行实例 »
点击 "运行实例" 按钮查看在线实例
作业4: 数据库的连接与检测(至少写5遍以上,写到吐为止),将连接参数写到独立的配置文件,要求配置参数必须用数组来实现,并在连接脚本中正确引用。
实例
<?php /** mysqli 的连接信息,用数组实现 */ $dbinfo = ['host'=>'localhost','user'=>'root','pwd'=>'root','dbname'=>'stu']; /* * host :$db_host 对应 * user:$db_user 对应 * pwd:$db_pass 对应 * dbname:$db_name 对应 */
运行实例 »
点击 "运行实例" 按钮查看在线实例
实例
<?php /** 实现MySQL的连接 */ //error_reporting(E_ALL ^E_WARNING); 用来忽略报错的,除了^E_warning 级别的都给错。 require 'config.php'; //引入MySQL的连接信息; $mysqli = new mysqli($dbinfo['host'],$dbinfo['user'],$dbinfo['pwd'],$dbinfo['dbname']); // 用mysqli的构造方法 new了一个$mysqli 对象,传入的参数分别是:数据的主机名,登录用户名,用户密码,默认连接的数据库 // var_dump($mysqli) 打印一下MySQLi的信息 if ($mysqli->connect_errno){ die('连接错误信息:'.$mysqli->connect_errno.':'.$mysqli->connect_error);//如果错误,显示错误信息 } echo '给个提示,连接成功了'; //$mysqli->set_charset('utf8'); 设置clint默认编码字符集为uft8 //$mysqli->select_db($dbinfo['dbname']); 选择默认的连接数据库;
运行实例 »
点击 "运行实例" 按钮查看在线实例
作业5:与MySQLil连接相关的几个属性和方法的名称,参数,与功能和用法
mysql类, $mysqli->errno, $mysqli->error, $mysqli->select_db(), $mysqli->set_charset(),