level 2
MDXBD
楼主
<?php
// include **;
header("content-type:text/html;charset=utf-8");
class Mysql{
public $host = *localhost*; // 主机
public $name = *root*; //用户名
public $pass = **; //密码
public $db = *test*; //数据库
public $table; //数据表
public $sql; //拼接的sql语句
public $where; //where条件
public $delete; //delete删除
public function __construct(){
mysql_connect($this->host,$this->name,$this->pass); //链接数据库
mysql_select_db($this->db); //选择数据库
mysql_set_charset(*utf8*); //默认utf8字符集
}
/* 选择数据表 */
public function table($table = **){
$this->table = $table;
return $this; //返回对象。
}
public function select($data,$success=true){
if($success == true){
}else{
}
}
public function getAll($data = ***){
$sql = "select $data from $this->table";
$result = $this->send($sql);
$array = $this->whil($result);
return $array;
}
public function whil($result){
while($row = mysql_fetch_assoc($result)){
$array[] = $row;
}
return $array;
}
/* 插入数据 $data 是一个数组 */
public function insert($data,$success = true){
if($success == true){
if(is_string($data)){ //如果是字符串就转换成数组的形式。
// 待处理。
}
if(is_array($data)){
$key = implode(*,*,array_keys($data));
$val = array_values($data);
foreach ($val as $v) {
$k[] = "\"".addslashes($v)."\""; //转义值中的单双引号。
}
$val = implode(*,*,$k);
$sql = "insert into $this->table ($key) values ($val)";
$result = $this->send($sql);
return mysql_insert_id();
}
}else{
$result = $this->send($data); //为false就是自己插入数据。原生sql语句。
return mysql_insert_id();
}
}
public function update($data,$success=true){
if($success == true){
if(is_array($data)){
foreach ($data as $key => $value) {
$str[] = $key.*=*."\"".addslashes($value)."\"";
}
$str = implode(*,*,$str);
$this->update = "update $this->table set $str";
return $this;
}
}else{
$result = $this->send($data); //为false就是自己插入数据,原生sql语句。
return mysql_affected_rows();
}
}
public function delete($data = **,$success = true){
if($success == true){
if($data == **){ //当delete中没有数据时在后面的where中写数据
$this->delete = "delete from $this->table";
return $this;
}else{ //当delete中有条件的时候
$this->send("delete from $this->table where $data");
return mysql_affected_rows(); //受影响的行数。
}
}else{
$result = $this->send($data);
return mysql_affected_rows(); //返回受影响的行数。
}
}
public function where($where){
if(!empty($this->update)){
$result = $this->send("$this->update where $where");
return mysql_affected_rows();
}else if(!empty($this->delete)){
$result = $this->send("$this->delete where $where");
return mysql_affected_rows();
}
}
public function send($sql){
$result = mysql_query($sql);
return $result; //返回资源。
}
}
$mysql = new Mysql;
// insert 插入数据,
// $data[*name*] = *我是季ddddddddggggg基金登记*;
// $data[*email*] = *[email protected]*;
// $id = $mysql->table(*user*)->insert($data);
// $id = $mysql->table(*user*)->insert(array(*name*=>*aaaaaaaaaa*));
//自己写sql语句,但是后面要加上 false 默认true,
// $id = $mysql->table(*user*)->insert("insert into user (name,email) values (*11111111111*,*22222222222*) ",false);
// echo $id;
//更新数据。
// $data[*name*] = *我是季我是被更新的基金登记*;
// $data[*email*] = *1dk我是被更新的[email protected]*;
// $id = $mysql->table(*user*)->update($data)->where("id=9");
//自己拼写语句。后面加上false
// $id = $mysql->table(*user*)->update("update user set name=*1111wwwd11111*,email=*22ddddrrrrwwwrr2222* where id=1 ",false);
// echo $id;
//删除数据
// $id = $mysql->table(*user*)->delete("id = 7");
// $id = $mysql->table(*user*)->delete()->where("id = 9");
//自己拼写语句。后面加上false
// $id = $mysql->table(*user*)->delete("delete from user where id=11",false);
// echo $id;
//查询数据
// $result = $mysql->table(*user*)->getAll(); //查询所有数据
// $result = $mysql->table(*user*)->getAll(*name,email*);
//-----------------------------------------------------------------------------------------------------
//select 情况下where limit In order by需要return $this 如何解决??,自动判断?????如何拼接??
$result = $mysql->table(*user*)->where()->limit();
echo *<pre>*;
print_r($result);
echo *</pre>*;
//select 下where limit In order by需要return $this 自动拼节?
/*
drop table user;
CREATE TABLE `user` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
email varchar(255) not null,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
select * from user;
*/
2014年11月10日 07点11分
1
// include **;
header("content-type:text/html;charset=utf-8");
class Mysql{
public $host = *localhost*; // 主机
public $name = *root*; //用户名
public $pass = **; //密码
public $db = *test*; //数据库
public $table; //数据表
public $sql; //拼接的sql语句
public $where; //where条件
public $delete; //delete删除
public function __construct(){
mysql_connect($this->host,$this->name,$this->pass); //链接数据库
mysql_select_db($this->db); //选择数据库
mysql_set_charset(*utf8*); //默认utf8字符集
}
/* 选择数据表 */
public function table($table = **){
$this->table = $table;
return $this; //返回对象。
}
public function select($data,$success=true){
if($success == true){
}else{
}
}
public function getAll($data = ***){
$sql = "select $data from $this->table";
$result = $this->send($sql);
$array = $this->whil($result);
return $array;
}
public function whil($result){
while($row = mysql_fetch_assoc($result)){
$array[] = $row;
}
return $array;
}
/* 插入数据 $data 是一个数组 */
public function insert($data,$success = true){
if($success == true){
if(is_string($data)){ //如果是字符串就转换成数组的形式。
// 待处理。
}
if(is_array($data)){
$key = implode(*,*,array_keys($data));
$val = array_values($data);
foreach ($val as $v) {
$k[] = "\"".addslashes($v)."\""; //转义值中的单双引号。
}
$val = implode(*,*,$k);
$sql = "insert into $this->table ($key) values ($val)";
$result = $this->send($sql);
return mysql_insert_id();
}
}else{
$result = $this->send($data); //为false就是自己插入数据。原生sql语句。
return mysql_insert_id();
}
}
public function update($data,$success=true){
if($success == true){
if(is_array($data)){
foreach ($data as $key => $value) {
$str[] = $key.*=*."\"".addslashes($value)."\"";
}
$str = implode(*,*,$str);
$this->update = "update $this->table set $str";
return $this;
}
}else{
$result = $this->send($data); //为false就是自己插入数据,原生sql语句。
return mysql_affected_rows();
}
}
public function delete($data = **,$success = true){
if($success == true){
if($data == **){ //当delete中没有数据时在后面的where中写数据
$this->delete = "delete from $this->table";
return $this;
}else{ //当delete中有条件的时候
$this->send("delete from $this->table where $data");
return mysql_affected_rows(); //受影响的行数。
}
}else{
$result = $this->send($data);
return mysql_affected_rows(); //返回受影响的行数。
}
}
public function where($where){
if(!empty($this->update)){
$result = $this->send("$this->update where $where");
return mysql_affected_rows();
}else if(!empty($this->delete)){
$result = $this->send("$this->delete where $where");
return mysql_affected_rows();
}
}
public function send($sql){
$result = mysql_query($sql);
return $result; //返回资源。
}
}
$mysql = new Mysql;
// insert 插入数据,
// $data[*name*] = *我是季ddddddddggggg基金登记*;
// $data[*email*] = *[email protected]*;
// $id = $mysql->table(*user*)->insert($data);
// $id = $mysql->table(*user*)->insert(array(*name*=>*aaaaaaaaaa*));
//自己写sql语句,但是后面要加上 false 默认true,
// $id = $mysql->table(*user*)->insert("insert into user (name,email) values (*11111111111*,*22222222222*) ",false);
// echo $id;
//更新数据。
// $data[*name*] = *我是季我是被更新的基金登记*;
// $data[*email*] = *1dk我是被更新的[email protected]*;
// $id = $mysql->table(*user*)->update($data)->where("id=9");
//自己拼写语句。后面加上false
// $id = $mysql->table(*user*)->update("update user set name=*1111wwwd11111*,email=*22ddddrrrrwwwrr2222* where id=1 ",false);
// echo $id;
//删除数据
// $id = $mysql->table(*user*)->delete("id = 7");
// $id = $mysql->table(*user*)->delete()->where("id = 9");
//自己拼写语句。后面加上false
// $id = $mysql->table(*user*)->delete("delete from user where id=11",false);
// echo $id;
//查询数据
// $result = $mysql->table(*user*)->getAll(); //查询所有数据
// $result = $mysql->table(*user*)->getAll(*name,email*);
//-----------------------------------------------------------------------------------------------------
//select 情况下where limit In order by需要return $this 如何解决??,自动判断?????如何拼接??
$result = $mysql->table(*user*)->where()->limit();
echo *<pre>*;
print_r($result);
echo *</pre>*;
//select 下where limit In order by需要return $this 自动拼节?
/*
drop table user;
CREATE TABLE `user` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
email varchar(255) not null,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
select * from user;
*/