Blogger Information
Blog 48
fans 0
comment 0
visits 40786
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
类与对象实例化及mysqli数据连接操作—2018年08月29日22时00分
小星的博客
Original
780 people have browsed it

今天是第十四天上课,朱老师从类与对象讲到了数据库连接操作。

重点知识:1.类就是对象的模板,对象就是类的一个实例。

                 2.构造方法__construct()会在类实例化时自动调用。构造方法参数必须要传入。

                 3.sql语句的增改删查。

                 4.mysqli数据库连接用connect_errno/connect_error/error/errno来判断连接是否成功。

1. 类与对象

类就是对象的模板

对象就是类的一个实例。

举例:人类就是一个类,我们每个人就是一个对象实例

2. 类定义与实例化

代码:

实例

<?php
class Employee5
{
    private $name;
    private $age;
    private $salary;
    private $sex;
    private $data = ['name','age','salary','sex'];

    //构造方法,初始化当前类成员,在类实例化时自动调用
    public function __construct($a,$b,$c,$d)
    {
        $this->name = $a;
        $this->age = $b;
        $this->salary = $c;
        $this->sex = $d;
    }

    //一般一个属性对应一个设置器与获取器

    //创建对外访问的公共接口
    // 类中用双下划线的方法是系统定义,由系统自动调用,叫魔术方法
    public function __get($member)
    {
        $msg = null;
        if(isset($member)){
            return $this->$member;
        }elseif(isset($data[$member])){
            return $data[$member];
        }else{
            $msg = '无此属性';
        }
        return $msg;
    }

    //设置器
    public function __set($member,$value)
    {
        $this->$member = $value;
    }










}

运行实例 »

点击 "运行实例" 按钮查看在线实例

实例化:

实例

<?php
header('Content-type:text/html;charset=utf-8');

require('class/Employee5.php');

$employee5 = new Employee5('ddss',12,[12,232,245],'nan');
//echo $employee5->getName('zmx');//不能访问

echo $employee5->sex,'<br>';
echo $employee5->age,'<br>';
echo $employee5->name,'<br>';

$employee5->name = 'admin';
echo $employee5->nam

运行实例 »

点击 "运行实例" 按钮查看在线实例

3. SQL语句(增改删查)

INSERT INTO `staff` (`name`,`sex`,`age`,`salary`) VALUES('小红',1,25,2000)

INSERT INTO `staff` (`name`,`sex`,`age`) VALUES('小花',1,25)

INSERT INTO `staff` (`name`,`salary`) VALUES('uer3',6500)

INSERT INTO `staff` (`name`,`age`,`sex`,`salary`) VALUES('woman',23,1,2000)

INSERT INTO `staff` (`sex`,`age`,`salary`) VALUES(1,25,2000)

 

UPDATE `staff` SET `name` = 'newuser' WHERE `id` = 22

UPDATE `staff` SET `name` = 'zmx' WHERE `salary` = 2000

UPDATE `staff` SET `sex` = 1 WHERE `id` = 23

UPDATE `staff` SET `age` = '66' WHERE `name` = 'newuser'

UPDATE `staff` SET `salary` = '5000' WHERE `id` = 16

 

DELETE FROM `staff` WHERE `id` = 24

DELETE FROM `staff` WHERE `name` = 'username'

DELETE FROM `staff` WHERE `sex` = 0

DELETE FROM `staff` WHERE `age` = 23

DELETE FROM `staff` WHERE `salary` > 2400

 

SELECT `NAME` FROM `staff` WHERE `id`>10

SELECT `NAME` ,`sex` FROM `staff` WHERE `id`=23

SELECT `NAME` ,`sex`,`age`,`salary` FROM `staff` WHERE `id`=25

SELECT COUNT(*) FROM `staff`

SELECT * FROM `staff` WHERE `id`>10

4. 数据库连接

代码:

实例

<?php
header('Content-type:text/html;charset=utf-8');

$mysqli = new mysqli($db['host'],$db['user'],$db['pass'], $db['dbname']);//看来只能有四个参数

error_reporting(E_ALL^E_WARNING);//屏蔽掉warning错误
//判断是否连接成功?
if ($mysqli->connect_errno) {
    // 自定义错误提示信息
    die('连接错误'.$mysqli->connect_errno.': '. $mysqli->connect_error);
}


echo '<h1>连接成功</h1>';

// 设置默认数据库
//$mysqli->select_db($db_name);

//设置默认的字符编码集
$mysqli->set_charset($db_charset);

运行实例 »

点击 "运行实例" 按钮查看在线实例

config文件:

实例

<?php
$db = [
    'host' => '127.0.0.1',
    'user' => 'root',
    'pass' =>'root',
    'dbname' => 'php',
    'charset' => 'utf8'
];

$host = '127.0.0.1';
$user = 'root';
$pass= 'root';
$dbname = 'php';
$charset = 'utf8';
?>

运行实例 »

点击 "运行实例" 按钮查看在线实例

 6.mysqli连接相关属性方法

作业.png


Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments