本款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 www.111cn.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 emptyempty($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(emptyempty($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 errorwww.111cn.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 /> ";
- }//开源代码Vevb.com
- }
- else {
- echo "no results where found";
- }
新闻热点
疑难解答