php连接mysql数据库类
这是一款简单实用的php连接mysql数据库类了,只做了数据查询,返回数组集,获取新的id 取得记录行等简单的mysql数据库连接代码,代码如下:
- class database {
-
- var $dblink;
-
- function connect($dbhost, $dbuser, $dbpw, $dbname = "") {
- $this->dblink = mysql_connect($dbhost, $dbuser, $dbpw);
-
- mysql_query("set names 'utf8'");
- mysql_query("set character_set_client=utf8");
- mysql_query("set character_set_results=utf8");
-
- if($dbname) {
- mysql_select_db($dbname, $this->dblink);
- }
- }
-
- function query($sql) {
- $result = mysql_query($sql, $this->dblink);
- return $result;
- }
-
- function fetch_array($result) {
- return mysql_fetch_array($result);
- }
-
- function insert_id() {
- $id = mysql_insert_id();
- return $id;
- }
-
- function getrow($sql) {
- $result = mysql_query($sql, $this->dblink);
- return mysql_fetch_assoc($result);
- }
-
- function getdetailrow($sql) {
- $result = mysql_query($sql, $this->dblink);
- return mysql_fetch_array($result);
- }
-
- function close() {
- mysql_close($this->dblink);
- }
- }
PHP调用方法,代码如下:
- $db = new database;
- $db->connect($dbhost, $dbuser, $dbpw, $dbname);
- unset($dbhost, $dbuser, $dbpw, $dbname);