看不懂你写的什么。给个现成的你
/**
* Created by PhpStorm.
* User: TAOYU
* Date: 14-11-16
* Time: 上午1:28
*/
class mysql
{
protected $host;
protected $user;
protected $pwd;
protected $port;
protected $error;
protected $db;
protected $charset;
protected $conn = null;
public static $total;//获得总条数
public static $pages;//总页数
public static $pagesize;//每页显示条数
public $act_page;//获取当前页码
public $start;//开始条数
//构造方法,初始化时连接数据库
public function __construct($h = 'localhost', $u = 'root', $pwd = '123', $port = 3306)
{
$this->host = $h;
$this->user = $u;
$this->pwd = $pwd;
$this->port = $port;
$this->connect();
$this->selectDb('bookboss');
$this->setChar('utf8');
}
public function __destruct()
{
mysql_close();
}
//连接方法
public function connect()
{
if ($this->conn = mysql_connect($this->host, $this->user, $this->pwd, $this->port)) {
return true;
} else {
$this->error = "连接失败!";
return false;
}
}
//选库方法
public function selectDb($dbName)
{
//use后要有空格!!!注意!!!
$sql = "use " . $dbName;
$this->db = $dbName;
return $this->query($sql);
}
//设置字符集方法
public function setChar($char)
{
//set names后要有空格!!!注意!!!
$sql = "set names " . $char;
return $this->query($sql);
}
//查询方法
public function query($sql)
{
$rs = mysql_query($sql, $this->conn);
if (!$rs) {
/*$this->error=mysql_error($this->conn);
$this->log($this->error);*/
return false;
} else {
return $rs;
}
}
//取指定数据
/* public function getData($page,$pagesize=5)
{
$start=($page-1)*$pagesize;
$rs = $this->query("select * from student limit $start,$pagesize");
if (!$rs) {
return false;
} else {
$list = array();
while ($row = mysql_fetch_assoc($rs)) {
$list[] = $row;
}
return $list;
}
}*/
//取数据
public function getAll($sql)
{
$rs = $this->query($sql);
if (!$rs) {
return false;
} else {
$list = array();
while ($row = mysql_fetch_assoc($rs)) {
$list[] = $row;
}
return $list;
}
}
//返回sql语句结果条数
public function getNums($sql){
return mysql_num_rows($this->query($sql));
}
//insert插入数据方法
public function insert($sql)
{
}
//读取错误方法
public function getError()
{
return $this->error;
}
//记录日志方法
/* public function log($err){
$time=date('Y-m-d H:i:s',time());
if(file_exists("./log.txt")){
$contents=file_get_contents("./log.txt");
$contents.="$time\t".$err."\r\n";
file_put_contents('./log.txt',$contents);
}else{
$filename='./log.txt';
$str='';
writefile($filename,$str);
$contents=file_get_contents("./log.txt");
$contents.="$time\t".$err."\r\n";
file_put_contents('./log.txt',$contents);
}
}*/
}