在WEB开发中php连接mysql数据库是肯定会用到的,如果你不会连接数据库就等不会WEB,我们提供这一款连接mysql数据库的类文件,可以方便快捷使php与mysql建立连接,代码如下:
- * created on 2010-4-21
- *
- * the class for control mysql
- *
- * made by s71ence
- *
- * @$host
- * @$user_name
- * @$user_pwd
- * @$data_base
- * @$coding
- */
- class mysql
- {
- private $host;//主机名
- private $user_name;//用户名
- private $user_pwd;//密码
- private $data_base;//数据库名
- private $coding;//编码
- //构造函数 进行初始化操作
- function __construct($host,$user_name,$user_pwd,$data_base,$coding)
- {
- $this->host=$host;
- $this->user_name=$user_name;
- $this->user_pwd=$user_pwd;
- $this->data_base=$data_base;
- $this->coding=$coding;
- $this->connect();//初始化连接
- }
- /*********************************************************************************************
- * 数据库
- * 基本方法
- ********************************************************************************************/
- //数据库连接
- function connect()
- {
- $link=mysql_connect($this->host,$this->user_name,$this->user_pwd) or die($this->error());
- mysql_select_db($this->data_base,$link) or die("无法连接数据库".$this->data_base);
- mysql_query("set names '$this->coding'");
- }
- //错误信息
- function error()
- {
- return mysql_error();
- }
- //mysql_query()方法
- function query($sql, $type = '')
- {
- if(!($query = mysql_query($sql)))
- {
- $this->show('say:', $sql);
- }
- //echo $sql."<br/>";//测试完成后 注释
- return $query;
- }
- //sql语句显示
- function show($message = '', $sql = '')
- {
- if(!$sql)
- {
- echo $message;
- }
- else
- {
- echo $message.'<br>'.$sql;
- }
- }
- //mysql_affected_rows()方法
- function affected_rows()
- {
- return mysql_affected_rows();
- }
- //mysql_result方法
- function result($query, $row)
- {
- return mysql_result($query, $row);
- }
- //mysql_num_rows方法
- function num_rows($query)
- {
- return @mysql_num_rows($query);
- }
- //mysql_num_fields方法
- function num_fields($query)
- {
- return mysql_num_fields($query);
- }
- //mysql_free_result方法
- function free_result($query)
- {
- return mysql_free_result($query);
- }
- //mysql_insert_id方法
- function insert_id()
- {
- return mysql_insert_id();
- }
- //mysql_fetch_row方法
- function fetch_row($query)
- {
- return mysql_fetch_row($query);
- }
- //mysql_get_server_info方法
- function version()
- {
- return mysql_get_server_info();
- }
- //mysql_fetch_array()方法
- function fetch_array($result)
- {
- return mysql_fetch_array($result);
- }
- //mysql_close方法
- function close()
- {
- return mysql_close();
- }
- /*********************************************************************
- * 数据库
- * 功能方法
- *********************************************************************/
- /*
- * insert方法
- * $table 表名
- * $fields 字段名
- * $value 字段值
- */
- function fn_insert($table,$fields,$values)
- {
- return $this->query("insert into $table ($fields) values ($values)");
- $this->close();
- }
- /*
- * select方法
- * $table 表名
- * $fields 字段名
- * $condition 查询条件
- * $order 排序条件
- * $limit 取出条数
- */
- function fn_select($table,$fields,$condition,$order,$limit)
- {
- $query="select $fields from $table";
- if($condition!="")
- {
- $query.=" where $condition";
- }
- if($order!="")
- {
- $query.=" order by $order ";
- }
- if($limit!="")
- {
- $query.=" limit $limit";
- }
- return $this->query($query);
- $this->close();
- }
- /*
- * delete方法
- * $table 表名
- * $fields 字段名
- * $values 字段值
- */
- function fn_delete($table,$condition)
- {
- return $this->query("delete from $table where $condition");
- $this->close();
- }
- /*
- * update方法
- * $table 表名
- * $fields 字段名
- * $values 字段值
- */
- function fn_update($table,$set,$condition)
- {
- $sql="update $table set $set";
- if($condition!="")
- {
- $sql.=" where $condition";
- }//开源代码Vevb.com
- return $this->query($sql);
- $this->close();
- }
- /*
- * 析构函数,垃圾回收
- */
- function __destruct()
- {
- //echo "clear";
- }
- }
调用方法,代码如下:
$db = new mysql('127.0.0.1','username','password','databasename',"utf8");
新闻热点
疑难解答