本款php连接mysql数据库连接程序代码是一款比较简单实用的连接代码,希望本教程对各位同学会有所帮助,代码如下:
class mysql { public $sqlserver = 'localhost' ; public $sqluser = 'root' ; public $sqlpassword = '' ; public $database ; public $last_query = '' ; private $connection ; private $query_result ; public function __construct() {} public function __destruct() { $this ->close(); } //+======================================================+ // create a connection to the mysql database //+======================================================+ public function connect( $server = null, $user = null, $password = null, $database = null){ if (isset( $server )) $this ->sqlserver = $server ; if (isset( $user )) $this ->sqluser = $user ; if (isset( $password )) $this ->sqlpassword = $password ; if (isset( $database )) $this ->database = $database ; $this ->connection = mysql_connect( $this ->sqlserver, $this ->sqluser, $this ->sqlpassword); if ( $this ->connection){ if (mysql_select_db( $this ->database)){ return $this ->connection; } else { return $this ->error(); } } else { return $this ->error(); } } //+======================================================+ // execute a query //+======================================================+ public function query( $query , $die = false){ if ( $query != null){ $this ->last_query = $query ; $this ->query_result = mysql_query( $query , $this ->connection); if (! $this ->query_result){ if ( $die ) die ( "die: " . $this ->query_result); return $this ->error(); } else { if ( $die ) die ( "die: " . $this ->query_result); return $this ->query_result; } } else { echo "empty query cannot be executed!" ; } } //+======================================================+ // returns the result //+======================================================+ public function getresult(){ return $this ->query_result; } //+======================================================+ // returns the connection //+======================================================+ public function getconnection(){ return $this ->connection; } //+======================================================+ // returns an object with properties rep // resenting the result fields HdhCmsTest111cn.net and values //+======================================================+ public function getobject( $qry = null){ if (isset( $qry )) $this ->query( $qry ); return mysql_fetch_object( $this ->getresult()); } //+======================================================+ // returns an array with keys representi // ng the result fields and values //+======================================================+ public function getarray( $query_id = "" ){ if ( $query_id == null){ $return = mysql_fetch_array( $this ->getresult()); } else { $return = mysql_fetch_array( $query_id ); } return $return ? $return : $this ->error(); } //+======================================================+ // returns the number of rows in the res // ult //+======================================================+ public function getnumrows( $qry = null){ if (isset( $qry )) $this ->query( $qry ); $amount = mysql_num_rows( $this ->getresult()); return empty empty ( $amount ) ? 0 : $amount ; } //+======================================================+ // returns if the result contains rows //+======================================================+ public function hasresults( $qry = null) { if (isset( $qry )) $this ->query( $qry ); return $this ->getnumrows( $qry ) > 0; } //+======================================================+ // returns the number of rows that where // affected by the last action //+======================================================+ public function getaffectedrows( $qry = null, $query_id = null){ if (isset( $qry )) $this ->query( $qry ); if ( empty empty ( $query_id )){ $return = mysql_affected_rows( $this ->getresult()); } else { $return = mysql_affected_rows( $query_id ); } return $return ? $return : $this ->error(); } //+======================================================+ // returns the auto generated id from th // e last insert action //+======================================================+ public function getinsertid( $connection_link = null){ return mysql_insert_id(isset( $connection_link ) ? $connection_link : $this ->connection); } //+======================================================+ // close the connection to the mysql dat // abase //+======================================================+ public function close(){ if (isset( $this ->connection)){ return @mysql_close( $this ->connection); } else { return $this ->error(); } } //+======================================================+ // outputs the mysql error //+======================================================+ private function error(){ if (mysql_error() != '' ){ echo '<b>mysql errorHdhCmsTest111cn.net</b>: ' .mysql_error(). '<br/>' ; } } } //demo // database object initialization $db = new mysql(); $db ->connect( "localhost" , "root" , "123456" , "user" ); // update query //$db->query("update table_name set field_name = value where another_field = another_value"); // select with check for record amount if ( $db ->hasresults( "select * from userinfo" )) { // loop through the user records, and get them as objects // note that the getobject method will use the last executed query when not provided with a new one while ( $user = $db ->getobject()) { echo "user $user->username is called $user->password<br /> " ; } //开源代码phpfensi测试数据 } else { echo "no results where found" ; }声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://haodehen.cn/did29598