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 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($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 /> ";
  }
}
else {
  echo "no results where found";
}

时间: 2024-10-22 12:03:02

mysql数据库连接类的相关文章

一款实用的php mysql数据库连接类

 本款数据库连接类,他会自动加载sql防注入功能,过滤一些敏感的sql查询关键词,同时还可以增加判断字段 show table status的性质与show table类 获取数据库所有表名等.*/ @ini_set('mysql.trace_mode','off'); class mysql {  public $dblink;  public $pconnect;  private $search = array('/union(s*(/*.**/)?s*)+select/i', '/loa

mysql 数据库连接类

mysql教程 数据库教程连接类  /*  提供一款简单实用的mysql php教程数据库连接代码哦,如果你正是php 入门者可以进来看看这款数据库代码是不是你要找的吧.  */ class mysql{     private $host;     private $user;     private $pass;     private $dbname;     function __construct($host,$user,$pass,$dbname){         $this->h

一个常用php mysql数据库连接类

 代码如下 复制代码 <?php  class ConnectionMySQL{      //主机      private $host="localhost";      //数据库的username      private $name="root";      //数据库的password      private $pass="";      //数据库名称      private $table="phptest&qu

通用mysql数据库连接类代码

数据库连接是一种有限的昂贵的资源,数据库连接影响到程序的性能指标.数据库连接池正是针对这个问题提出来的.数据库连接池负责分配.管理和释放数据库连接,它允许应用程序重复使用一个现有的数据库连接,而再不是重新建立一个:释放空闲时间超过最大空闲时间的数据库连接来避免因为没有释放数据库连接而引起的数据库连接遗漏.这项技术能明显提高对数据库操作的性能. /*  * created on 2010-3-8  * make by:suniteboy  * my first mysql class  *  */

php mysql数据库连接类

<?php class mysqlquery {  var $sql;//sql语句执行结果  var $query;//sql语句  var $num;//返回记录数  var $r;//返回数组  var $id;//返回数据库id号  //执行mysql_query()语句  function query($query)  {   $this->sql=mysql_query($query) or die(mysql_error()."<br>".$que

实用简单的mysql 数据库连接类

class DB {  //database connection  var $con = FALSE;  function DB($MYSQL_HOST=MYSQL_HOST, $MYSQL_USER=MYSQL_USER, $MYSQL_PASS=MYSQL_PASS,$MYSQL_DB=MYSQL_DB)  {   $this->con = @mysql_connect($MYSQL_HOST, $MYSQL_USER, $MYSQL_PASS) or die("Could not

php mysql数据库连接类程序代码

 代码如下 复制代码 class cls_mysql{ var $querynum = 0; var $link; var $histories; var $dbhost; var $dbuser; var $dbpw; var $dbcharset; var $pconnect; var $tablepre; var $time; var $goneaway = 5; function connect($dbhost, $dbuser, $dbpw, $dbname = '', $dbchar

深入理解php的MySQL连接类_php技巧

无意间在电脑里发现还有这么个Mysql的连接类,也不记得哪里收藏的了,贴上来吧.后面几个show_databases和show_tables....等方法都用了一堆echo,好像一直不喜欢在类的方法里直接用输出语句,不过这也只是列举数据库和表名,构造函数的参数也可以给个默认值吧. 复制代码 代码如下: <?php/** filename:mysql数据库连接类*/class mysql{ private $db_host;  //数据库主机 private $db_user;  //数据库用户名

mysql-Java Web连接MySql数据库,修改数据库连接类中的用户名和密码没有效果?

问题描述 Java Web连接MySql数据库,修改数据库连接类中的用户名和密码没有效果? 现在在做一个项目,其中编写了数据库连接及操作的类. 1.ConnDB类. package com.wgh.tools; import java.io.InputStream; //导入java.io.InputStream类 import java.sql.*; //导入java.sql包中的所有类 import java.util.Properties; //导入java.util.Properties