<?php
class DbMysql {
public $conn;//$conn是类属性,用public修饰,不用public修饰是普通变量,类属性是对类变量的一种封装,此属性只给本类及其子类使用,所以,类属性=类变量(封装在类里的的变量)
function __construct() {
$this->conn = new mysqli("localhost","root","123456","shop");//将右边对象赋给左边对象的属性,即左边对象属性变成一个对象
$this->conn->query("set names utf8");
}
function select($sql,$fanhui=2) //$fanhui表示选中了几行
{
$result=$this->conn->query($sql);
if($result)
{
$array = array();
if($fanhui==1)
{
$array=$result->fetch_array();
}
else{
while($row=$result->fetch_array())
{
$array[]= $row;
}
}
return $array;
}
else
{
return false;
}
}
function sql($sql)
{
$this->conn->query($sql);
}
function affected()
{
return $this->conn->affected_rows;//mysqli->affected
}
}
date_default_timezone_set("PRC");//设置时区
?>
使用:
$db=new DbMysql();