Blogger Information
Blog 60
fans 1
comment 1
visits 64278
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
php利用静态成员制作数据库查询语句_2018年9月3日
PHP学习
Original
766 people have browsed it

实例

<meta http-equiv="Content-Type" content="text/html" charset="UTF-8">
<?php
/**
 * 类中的静态成员使用关键字:static来定义
 * 静态成员不能使用$this伪变量来访问
 * 访问静态成员使用::范围解析符来访问
 * 类中使用自身的关键字self::
 *
 * 范围解析符的作用:
 * 访问静态成员,访问类常量 ,继承上下文中引用被覆盖的成员
 */

class demo5
{
    public static $pdo = null;
    protected static $db=[
        'type' => 'mysql',
        'host' => '127.0.0.1',
        'dbname' => 'php',
        'user' => 'root',
        'pass' => 'root',
    ];
    public static function connect()
    {
        $dsn = self::$db['type'].':host='.self::$db['host'].';dbname='.self::$db['dbname'];
        self::$pdo = new PDO($dsn,self::$db['user'],self::$db['pass']);
    }
    public static function select($table,$fields='*',$num=5)
    {
        $stmt = self::$pdo->prepare("SELECT {$fields} FROM {$table} LIMIT {$num}");
        $stmt->execute();
        return $stmt->fetchALL(PDO::FETCH_ASSOC);
    }
}

//连接数据库
demo5::connect();

//查询数据表
$result = demo5::select('user','name,email','6');

//显示结果

echo '<pre>',var_export($result);
echo '<hr>';
//遍历数据
foreach ($result as $sumt)
{
    echo '姓名:'.$sumt['name'].' 年龄 :'.$sumt['email'].'<br>';
}

运行实例 »

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


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
Author's latest blog post