PHP4.0之后的版本很大的优势更加注重面向对象的编程。一般情况下,我们用一般方法进行数据库操作要分开写很多方法。用面向对象的方法会增强开发效率。下面给出面向对象编程的mysql数据库操作相关代码:
<?php
class mysql{
private $host;
private $root;
private $pwd;
private $database;
private $conn;
function __construct($host,$root,$pwd,$database){
$this->host = $host;
$this->root = $root;
$this->pwd = $pwd;
$this->database = $database;
$this->connect();
}
//连接数据库
function connect(){
$this->conn = mysql_connect($host,$root,$pwd);
mysql_select_db($this->database,$this->conn);
mysql_query("set names utf8");
}
//执行mysql语句
function query($sql){
return mysql_query($sql);
}
//返回一行数据
function arr_ay($result){
return mysql_fetch_array($result);
}
//返回结果集数目
function rows($reslt){
return mysql_num_rows();
}
function dbclose(){
mysql_close($this->conn);
}
//数据库的基本操作
//查询语句
function select($biao,$where){
return $this->query("SELECT * FROM '$biao' where" );
}
//插入语句
}