php连接mysql数据库类

这是一款简单实用的php连接mysql数据库类了,只做了数据查询,返回数组集,获取新的id 取得记录行等简单的mysql数据库连接代码,代码如下:

  1. class database {
  2. var $dblink;
  3. function connect($dbhost, $dbuser, $dbpw, $dbname = "") {
  4. $this->dblink = mysql_connect($dbhost, $dbuser, $dbpw);
  5. mysql_query("set names 'utf8'");
  6. mysql_query("set character_set_client=utf8");
  7. mysql_query("set character_set_results=utf8");
  8. if($dbname) {
  9. mysql_select_db($dbname, $this->dblink);
  10. }
  11. }
  12. function query($sql) {
  13. $result = mysql_query($sql, $this->dblink);
  14. return $result;
  15. }
  16. function fetch_array($result) {
  17. return mysql_fetch_array($result);
  18. }
  19. function insert_id() {
  20. $id = mysql_insert_id();
  21. return $id;
  22. }
  23. function getrow($sql) {
  24. $result = mysql_query($sql, $this->dblink);
  25. return mysql_fetch_assoc($result);
  26. }
  27. function getdetailrow($sql) {
  28. $result = mysql_query($sql, $this->dblink);
  29. return mysql_fetch_array($result);
  30. }
  31. //www.phpfensi.com
  32. function close() {
  33. mysql_close($this->dblink);
  34. }
  35. }

PHP调用方法,代码如下:

  1. $db = new database;
  2. $db->connect($dbhost, $dbuser, $dbpw, $dbname);
  3. unset($dbhost, $dbuser, $dbpw, $dbname);