php数据库连接类
本文章提供了数据库连接实例,主要是讲php mysql数据相关操作,有需要的朋友可以使用看看,实例代码如下:
- <?php
- class mysql {
- private $db_host;
- private $db_user;
- private $db_pass;
- private $db_name;
- private $db_charset;
- private $conn;
- public $debug=false;
- private $query_id;
- private $result;
- private $num_rows;
- private $insert_id;
-
- function __construct ($db_host,$db_user,$db_pass,$db_name,$db_charset,$conn) {
- $this->db_host = $db_host ;
- $this->db_user = $db_user ;
- $this->db_pass = $db_pass ;
- $this->db_name = $db_name ;
- $this->db_charset = $db_charset ;
- $this->conn = $conn ;
- $this->connect();
- }
- function __destruct () {
- @mysql_close($this->conn);
- }
-
- public function connect () {
- if ($this->conn == 'pconn') {
- @$this->conn = mysql_pconnect($this->db_host,$this->db_user,$this->db_pass);
- } else {
- @$this->conn = mysql_connect($this->db_host,$this->db_user,$this->db_pass);
- }
- if (!$this->conn) {
- $this->show_error('数据库-连接失败:用户名或密码错误!');
- }
- if (!@mysql_select_db($this->db_name,$this->conn)) {
- $this->show_error("数据库-选择失败:数据库 $this->db_name 不可用");
- }
- mysql_query("set names $this->db_charset");
- return $this->conn;
- }
-
- public function query ($sql) {
- if ($this->query_id) $this->free_result();
- $this->query_id = @mysql_query($sql,$this->conn);
- if (!$this->query_id) $this->show_error("sql语句 <b>"$sql"</b> 执行时遇到错误");
- return $this->query_id;
- }
-
- public function show_error ($msg) {
- if($this->debug){
- $errinfo = mysql_error();
- echo "错误:$msg <br/> 返回:$errinfo<p>";
- }else{
- echo '<p>出现错误!<p>';
- }
- }
-
- public function get_query_info($info){
- if ($this->query_id) {
- echo $info;
- }
- }
-
- public function findall ($table_name) {
- $this->query("select * from $table_name");
- }
-
- public function fetch_array () {
- if ($this->query_id) {
- $this->result = mysql_fetch_array($this->query_id);
- return $this->result;
- }
- }
-
- public function fetch_assoc () {
- if ($this->query_id) {
- $this->result = mysql_fetch_assoc($this->query_id);
- return $this->result;
- }
- }
- public function fetch_row () {
- if ($this->query_id) {
- $this->result = mysql_fetch_row($this->query_id);
- return $this->result;
- }
- }
- public function fetch_object () {
- if ($this->query_id) {
- $this->result = mysql_fetch_object($this->query_id);
- return $this->result;
- }
- }
-
- public function num_rows () {
- if ($this->query_id) {
- $this->num_rows = mysql_num_rows($this->query_id);
- return $this->num_rows;
- }
- }
-
- public function insert_id () {
- return $this->insert_id = mysql_insert_id();
- }
-
- public function show_tables () {
- $this->query("show tables");
- if ($this->query_id) {
- echo "数据库 $this->db_name 共有 ".$this->num_rows($this->query_id)." 张表<br/>";
- $i = 1;
- while ($row = $this->fetch_array($this->query_id)){
- echo "$i -- $row[0]<br/>";
- $i ++;
- }
- }
- }
-
- public function show_dbs(){
- $this->query("show databases");
- if ($this->query_id) {
- echo "共有数据库 ".$this->num_rows($this->query_id)." 个<br/>";
- $i = 1;
- while ($this->row = $this->fetch_array($this->query_id)){
- echo "$i -- ".$this->row[database]."<br />";
- $i ++;
- }
- }
- }
-
- public function drop_db ($db_name='') {
- if ($db_name == '') {
- $db_name = $this->db_name;
- $this->query("drop database $db_name");
- }else {
- $this->query("drop database $db_name");
- }
- if ($this->query_id) {
- return "数据库 $db_name 删除成功";
- }else {
- $this->show_error("数据库 $db_name 删除失败");
- }
- }
-
- public function drop_table ($table_name) {
- $this->query("drop table $table_name");
- if ($this->query_id) {
- return "数据表 $table_name 删除成功";
- }else {
- $this->show_error("数据表 $table_name 删除失败");
- }
- }
-
- public function create_db ($db_name) {
- $this->query("create database $db_name");
- if($this->query_id){
- return "数据库 $db_name 创建成功";
- }else {
- $this->show_error("数据库 $db_name 创建失败");
- }
- }
-
- public function get_info(){
- echo mysql_get_server_info();
- }
-
- public function free_result () {
- if ( @mysql_free_result($this->query_id) )
- unset ($this->result);
- $this->query_id = 0;
- }
- }
- ?>
下面提供一款自动选择数据库远程或本地连接代码:
- <?php
-
- include_once 'mysql.class.php';
-
- $mysql_local_data = array('db_host'=>'localhost',
- 'db_user'=>'root',
- 'db_pass'=>'root',
- 'db_name'=>'test');
-
- $mysql_remote_data = array('db_host'=>'61.183.41.178',
- 'db_user'=>'xxx',
- 'db_pass'=>'xxx',
- 'db_name'=>'xxx');
-
- $tb_prefix = 'php95_';
- $db_charset = 'utf-8';
-
- if (@mysql_connect($mysql_local_data[db_host], $mysql_local_data[db_user], $mysql_local_data[db_pass]))
- $db = new mysql($db_host, $mysql_local_data[db_user], $mysql_local_data[db_pass], $mysql_local_data[db_name], $db_charset, $conn);
- else
- $db = new mysql($mysql_remote_data[db_host], $mysql_remote_data[db_user], $mysql_remote_data[db_pass], $mysql_remote_data[db_name], $db_charset, $conn);
- $db->show_tables();
- ?>
假设我们要在test.php文件中操作虚拟主机的数据库,则首先要在本地调试,那么必然要连接本地、远程两个不同的数据库,问题:怎么让test.php自动识别当下该连接本地还是远程数据库呢?