<?php
class DB{
private $hostname;
private $username;
private $password;
private $select_db;
private $con;
private $Error;
public function __construct($hostname,$username,$password,$select_db){
if(!empty($hostname)&&!empty($username)&&!empty($select_db))//检查参数是否为空,否则不赋值
{
$this->hostname = $hostname;
$this->username = $username;
$this->password = $password;
$this->select_db = $select_db;
$this->con = mysql_connect($this->hostname,$this->username,$this->password);
if(!$this->con){
$this->Error = die('Could Not Connect:'.mysql_error);
}
else{
mysql_select_db($this->select_db,$this->con);
}
}
}
public function __destruct(){//退出时结束连接
mysql_close($this->con);
}
public function insert($table,$body){//插入table中的一个数组
$line1 = implode(',',$body);
$line2 = implode(',',array_keys($body));
echo $sql = "INSERT INTO $table ($line2) VALUES ($line1)";
$result = mysql_query($sql,$this->con);
if(!$result){
echo $this->Error;
echo '111';
}
}
public function update($table,$body){
}
public function read($table,$keyword){
}
public function delete($table,$keyword){
}
public function getLastError(){//返回最后一条错误信息
return $this->Error;
}
}
?>
主页调用的是<?php
require('DB.class.php');
$DB = new DB('localhost','root','','dbtest');
$line1 = array(
'aa'=>'`aa`',
'bb'=>'`dd`'
);
$DB->insert('1234',$line1);
?>
echo $sql语句是INSERT INTO 1234 (aa,bb) VALUES (`aa`,`dd`)
class DB{
private $hostname;
private $username;
private $password;
private $select_db;
private $con;
private $Error;
public function __construct($hostname,$username,$password,$select_db){
if(!empty($hostname)&&!empty($username)&&!empty($select_db))//检查参数是否为空,否则不赋值
{
$this->hostname = $hostname;
$this->username = $username;
$this->password = $password;
$this->select_db = $select_db;
$this->con = mysql_connect($this->hostname,$this->username,$this->password);
if(!$this->con){
$this->Error = die('Could Not Connect:'.mysql_error);
}
else{
mysql_select_db($this->select_db,$this->con);
}
}
}
public function __destruct(){//退出时结束连接
mysql_close($this->con);
}
public function insert($table,$body){//插入table中的一个数组
$line1 = implode(',',$body);
$line2 = implode(',',array_keys($body));
echo $sql = "INSERT INTO $table ($line2) VALUES ($line1)";
$result = mysql_query($sql,$this->con);
if(!$result){
echo $this->Error;
echo '111';
}
}
public function update($table,$body){
}
public function read($table,$keyword){
}
public function delete($table,$keyword){
}
public function getLastError(){//返回最后一条错误信息
return $this->Error;
}
}
?>
主页调用的是<?php
require('DB.class.php');
$DB = new DB('localhost','root','','dbtest');
$line1 = array(
'aa'=>'`aa`',
'bb'=>'`dd`'
);
$DB->insert('1234',$line1);
?>
echo $sql语句是INSERT INTO 1234 (aa,bb) VALUES (`aa`,`dd`)
解决方案 »
- php 求自动执行代码的思路和方法
- 为什么 我的本地可以正常执行 一旦上传 FTP 就不出现 d.php 答案呢? 是不是 数据库版本问题啊?
- 聊天室采用ajax轮询,如何避免数据重复或者遗漏?
- WinXP上安装与配置PHP后Apache Web Server 启动出错
- mysql in查询....
- 这个循环式怎么没有起作用呢?
- js获取php(ecshop smarty模板)数组元素值
- autoload()为什么传进去的参数居然带上了命名空间?
- 怎样用php调用web service
- 求sql写法,b2b网站搜索商家如何限制出现的次数?
- php向mysql插入数据传空值问题
- php上传视频并自动转码成FLV功能实现!
$DB = new DB('localhost','root','','dbtest');
$line1 = array(
'aa'=> "'aa'",
'bb'=> "'dd'"
);
$DB->insert('1234',$line1);
而且insert 语句还有一种格式:insert into tb_member set username = "test", type = 1, lastlogindt = now()。跟update样式差不多。
你也可以看看人家写的数据库类,我感觉挺好的:http://www.cnblogs.com/hooray/archive/2012/07/21/2603017.html