这是一款非常完整理的php连接mysql数据库,利用了php与mysql数据库进行连接,好了费话不说多了来看看这款经典的连接数据库代码是不是你想要找的吧,代码如下:
- class mysql {
- var $linkid=null;
- function __construct($dbhost, $dbuser, $dbpw, $dbname = '', $dbcharset = 'gbk', $connect = 1) {
- $this -> connect($dbhost, $dbuser, $dbpw, $dbname, $dbcharset, $connect);
- }
- function connect($dbhost, $dbuser, $dbpw, $dbname = '', $dbcharset = 'gbk', $connect=1){
- $func = emptyempty($connect) ? 'mysql_pconnect' : 'mysql_connect';
- if(!$this->linkid = @$func($dbhost, $dbuser, $dbpw, true)){
- $this->dbshow('can not connect to mysql!');
- } else {
- if($this->dbversion() > '4.1'){
- mysql_query( "set names gbk");
- if($this->dbversion() > '5.0.1'){
- mysql_query("set sql_mode = ''",$this->linkid);
- }
- }
- }
- if($dbname){
- if(mysql_select_db($dbname, $this->linkid)===false){
- $this->dbshow("can't select mysql database($dbname)!");
- }
- }
- }
- function select_db($dbname){
- return mysql_select_db($dbname, $this->linkid);
- }
- function query($sql){
- if(!$query=@mysql_query($sql, $this->linkid)){
- $this->dbshow("query error:$sql");
- }else{
- return $query;
- }
- }
- function getall($sql, $type=mysql_assoc){
- $query = $this->query($sql);
- while($row = mysql_fetch_array($query,$type)){
- $rows[] = $row;
- }
- return $rows;
- }
- function getone($sql, $type=mysql_assoc){
- $query = $this->query($sql,$this->linkid);
- $row = mysql_fetch_array($query, $type);
- return $row;
- }
- function fetch_array($result,$type = mysql_assoc){
- return mysql_fetch_array($result);
- }
- function affected_rows(){
- return mysql_affected_rows($this->linkid);
- }
- function num_rows(){
- return mysql_num_rows($this->linkid);
- }
- function num_fields($result){
- return mysql_num_fields($result);
- }
- function insert_id(){
- return mysql_insert_id($this->linkid);
- }
- function free_result(){
- return mysql_free_result($this->linkid);
- }
- function error(){
- return mysql_error($this->linkid);
- }
- function errno(){
- return mysql_errno($this->linkid);
- }
- function close(){
- return mysql_close($this->linkid);
- }
- function dbversion(){
- return mysql_get_server_info($this->linkid);
- }
- function dbshow($msg){
- if($msg){
- echo "error:".$msg."<br><br>";
- }else{
- echo "errno:".$this->errno()."<br>error:".$this->error();
- }
- exit;
- }
- }
|