PHP实现数据库连贯操作类 写了两个小时,终于完成了。首先声明,这个类写得还有很多问题,希望大家不要扔砖头。所以我把他发在基础编程里面。请大家多提意见,如果效果好,将一直更新。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 代码:<?php/* * auther:Evan Hou * descript: database operation class, come true coherence DB. * Data:2010-01-07 11:04 */class db_class{ public $CONN = ''; public $RES = ''; public $selectLang = ''; public $updateLang = ''; public $insertLang = ''; public $deleteLang = ''; function __construct() { $this->CONN = mysqli_connect('localhost', 'root', '123456', 'test'); } function __destruct() { if(is_object($this->RES)) mysqli_free_result($this->RES); mysqli_close($this->CONN); } function __set($name, $value) { echo $name = 'not find method!'; } function __get($name) { echo 'not define value! $'.$name.'<br>'; } function __call($name, $arguments) { echo 'not define method: '. $name.'<br>'; echo 'invalid paramater: '. implode(',', $arguments).'<br>'; } public function select($part) { $this->selectLang[] = 'select '.$part; return $this; } public function from($part) { $this->selectLang[] = 'from '.$part; $this->RES = mysqli_query($this->CONN, implode(' ', $this->selectLang)); return $this; } public function where($part) { $this->selectLang[] = 'where '.$part; $this->RES = mysqli_query($this->CONN, implode(' ', $this->selectLang)); return $this; } public function fetchArrayOne() { return mysqli_fetch_array($this->RES, MYSQLI_ASSOC); } public function fetchArrayAll() { $rows = array(); while($row = mysqli_fetch_array($this->RES, MYSQLI_ASSOC)) $rows[] = $row; return $rows; } public function fetchObjOne() { return mysqli_fetch_object($this->RES); } public function update($part) { $this->updateLang[] = 'update '.$part; return $this; } public function set($part) { $this->updateLang[] = 'set '.$part; return $this; } public function uWhere($part) { $this->updateLang[] = 'where '.$part; return $this; } public function execute() { mysqli_query($this->CONN, implode(' ', $this->updateLang)); } public function insert($part) { $this->insertLang[] = 'insert into '.$part; return $this; } public function fields($part) { $this->insertLang[] = '('.$part.')'; return $this; } public function values($part) { $this->insertLang[] = 'values'.'('.$part.')'; mysqli_query($this->CONN, implode(' ', $this->insertLang)); } public function delete() { $this->deleteLang[] = 'delete '; return $this; } public function dForm($part) { $this->deleteLang[] = 'from '.$part; return $this; } public function dWhere($part) { $this->deleteLang[] = 'where '.$part; mysqli_query($this->CONN, implode(' ', $this->deleteLang)); }}/** example* include DB select,update,insert,datele operation demo.* must has database table and configure connect database user with password. */$a = new db_class();//select example$a->select('*')->from('test')->where("name = 'joan'")->fetchArrayAll();//update example$a->update('test')->set("name = Evans")->uWhere("id = '5'");//insert example$a->insert('test')->fields('name,text')->values("'Alex', 'hello Alex'");//delete example$a->delete()->dForm('test')->dWhere("id = '4'");?>数据库:-- phpMyAdmin SQL Dump-- version 2.10.3-- http://www.phpmyadmin.net-- -- 主机: localhost-- 生成日期: 2010 年 01 月 07 日 03:08-- 服务器版本: 5.0.51-- PHP 版本: 5.2.6SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";-- -- 数据库: `test`-- -- ---------------------------------------------------------- -- 表的结构 `test`-- CREATE TABLE `test` ( `id` int(10) NOT NULL auto_increment, `name` varchar(10) character set utf8 collate utf8_unicode_ci NOT NULL, `text` varchar(10) character set utf8 collate utf8_unicode_ci NOT NULL, PRIMARY KEY (`id`)) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=15 ;-- -- 导出表中的数据 `test`-- INSERT INTO `test` (`id`, `name`, `text`) VALUES (1, 'evan', 'hello evan'),(2, 'joan', 'hello joan'),(3, 'cary', 'hello cary'),(14, 'Alex', 'hello Alex'); TPL模板文件可视化编辑问题 mysql怎么一次执行多条SQL语句? 怎样实现PHP中的静态地址转换 菜鸟问题 关于在线艺术签名 select出的图片乱码 跪求2:php函數操作LINUX系統的命令(無法執行的原因)(搞定再送100分,決不失言。) 菜鸟总结的PHP程序设计的十条经验(以论坛设计为例) 请问:一个http:错误,英雄,帮我! SOS:www.oso.com.cn不做了(在我申请了空间后不到三小时),气死我了!急急!!!! php 数组放进去的值,取出后不一致。 php如何在linux下读取doc内容
<?php
/*
* auther:Evan Hou
* descript: database operation class, come true coherence DB.
* Data:2010-01-07 11:04
*/
class db_class
{
public $CONN = '';
public $RES = '';
public $selectLang = '';
public $updateLang = '';
public $insertLang = '';
public $deleteLang = ''; function __construct()
{
$this->CONN = mysqli_connect('localhost', 'root', '123456', 'test');
} function __destruct()
{
if(is_object($this->RES)) mysqli_free_result($this->RES);
mysqli_close($this->CONN);
} function __set($name, $value)
{
echo $name = 'not find method!';
} function __get($name)
{
echo 'not define value! $'.$name.'<br>';
} function __call($name, $arguments)
{
echo 'not define method: '. $name.'<br>';
echo 'invalid paramater: '. implode(',', $arguments).'<br>';
} public function select($part)
{
$this->selectLang[] = 'select '.$part;
return $this;
} public function from($part)
{
$this->selectLang[] = 'from '.$part;
$this->RES = mysqli_query($this->CONN, implode(' ', $this->selectLang));
return $this;
} public function where($part)
{
$this->selectLang[] = 'where '.$part;
$this->RES = mysqli_query($this->CONN, implode(' ', $this->selectLang));
return $this;
} public function fetchArrayOne()
{
return mysqli_fetch_array($this->RES, MYSQLI_ASSOC);
} public function fetchArrayAll()
{
$rows = array();
while($row = mysqli_fetch_array($this->RES, MYSQLI_ASSOC)) $rows[] = $row;
return $rows;
} public function fetchObjOne()
{
return mysqli_fetch_object($this->RES);
}
public function update($part)
{
$this->updateLang[] = 'update '.$part;
return $this;
} public function set($part)
{
$this->updateLang[] = 'set '.$part;
return $this;
} public function uWhere($part)
{
$this->updateLang[] = 'where '.$part;
return $this;
} public function execute()
{
mysqli_query($this->CONN, implode(' ', $this->updateLang));
} public function insert($part)
{
$this->insertLang[] = 'insert into '.$part;
return $this;
} public function fields($part)
{
$this->insertLang[] = '('.$part.')';
return $this;
} public function values($part)
{
$this->insertLang[] = 'values'.'('.$part.')';
mysqli_query($this->CONN, implode(' ', $this->insertLang));
} public function delete()
{
$this->deleteLang[] = 'delete ';
return $this;
} public function dForm($part)
{
$this->deleteLang[] = 'from '.$part;
return $this;
} public function dWhere($part)
{
$this->deleteLang[] = 'where '.$part;
mysqli_query($this->CONN, implode(' ', $this->deleteLang));
}
}
/*
* example
* include DB select,update,insert,datele operation demo.
* must has database table and configure connect database user with password.
*/
$a = new db_class();//select example
$a->select('*')->from('test')->where("name = 'joan'")->fetchArrayAll();//update example
$a->update('test')->set("name = Evans")->uWhere("id = '5'");//insert example
$a->insert('test')->fields('name,text')->values("'Alex', 'hello Alex'");//delete example
$a->delete()->dForm('test')->dWhere("id = '4'");
?>
数据库:
-- phpMyAdmin SQL Dump
-- version 2.10.3
-- http://www.phpmyadmin.net
--
-- 主机: localhost
-- 生成日期: 2010 年 01 月 07 日 03:08
-- 服务器版本: 5.0.51
-- PHP 版本: 5.2.6SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";--
-- 数据库: `test`
-- -- ----------------------------------------------------------
-- 表的结构 `test`
-- CREATE TABLE `test` (
`id` int(10) NOT NULL auto_increment,
`name` varchar(10) character set utf8 collate utf8_unicode_ci NOT NULL,
`text` varchar(10) character set utf8 collate utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=15 ;--
-- 导出表中的数据 `test`
-- INSERT INTO `test` (`id`, `name`, `text`) VALUES
(1, 'evan', 'hello evan'),
(2, 'joan', 'hello joan'),
(3, 'cary', 'hello cary'),
(14, 'Alex', 'hello Alex');