<?php
/*
*class mysql
*
*/
class mysql{
private $host;
private $name;
private $password;
private $table;
private $ut;
//初始化对象
function __construct($host,$name,$password,$table,$ut){
$this->host=$host;
$this->name=$name;
$this->password=$password;
$this->table=$table;
$this->ut=$ut;
$this->connect();
}
//数据库连接方法
function connect(){
$link=mysql_connect($this->host,$this->name,$this->password) or die ($this->error());
mysql_select_db($this->table,$link) or die ("没有该数据库:".$this->table);
mysql_query("SET NAMES '$this->ut'");
}
//封装起来的基本操作
function query($v){
return mysql_query($v);
}
function error(){
return mysql_error();
}
function affected_rows(){
return mysql_affect_rows();
}
function result($query,$row){
return mysql_result($query,$row);
}
function num_rows($query){
return $mysql_num_rows($query);
}
function num_fields($query){
return mysql_num_fields($query);
}
function free_result($query){
return mysql_free_result($query);
}
function insert_id(){
return mysql_insert_id();
}
function fetch_row($query){
return mysql_fetch_row($query);
}
function version(){
return mysql_get_server_info();
}
function close(){
return mysql_close();
}
//封装起来的功能操作
//增
function fn_insert($table,$name,$value){
//insert into[表名] values('','',......顺序排列的数据);
$this->query("insert into $table ($name) values ($value)");
}
//删
function fn_del($table,$data){
//delete from [表名] where ([条件]);
$this->query("delete from $table where (id=$data)");
}
//查
function fn_search($table){
$this->query("select * from $table");
}
//改 UPDATE [表名] SET [修改内容如name = 'Mary'] WHERE [条件];
function fn_update($table,$name1,$value,$name2,$id){
$this->query("update $table set $name1='$value' where $name2='$id'");
}
}
//连接数据库
$db=new mysql('localhost','root','123456','db70100160',"GBK");
//增加记录
//$db->fn_insert('test','id,title,dates',"'6','我插入的信息',now()");
//删除记录
//$db->fn_del('test','1');
//查询记录
//$db->fn_search('test');
//修改记录
//$db->fn_update('test','title','laijian2','id','4');
?>
本文出自 “lai坚技术博客” 博客,谢绝转载!