mysql数据库连接类
发布:smiling 来源: PHP粉丝网 添加日期:2014-09-11 22:40:39 浏览: 评论:0
本款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 /> ";
- }//开源代码phpfensi.com
- }
- else {
- echo "no results where found";
- }
Tags: mysql数据库连接类
- 上一篇:php连接mysql数据库操作类
- 下一篇:php生成excel文件源代码
相关文章
- ·一个常用php mysql数据库连接类(2014-09-11)
- ·通用mysql数据库连接类代码(2014-09-11)
- ·mysql数据库连接类(2014-09-12)
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)