简单的MYsql操作类
- * 1、连接服务器 2、各类sql动作
- */
- class mysql{
- private $host;
- private $name;
- private $pass;
- private $table;
- private $jiema;
- private $ztime;
-
- function __construct($host,$name,$pass,$table,$jiema,$ztime){
- $this -> host = $host ;
- $this -> name = $name ;
- $this -> pass = $pass ;
- $this -> table = $table ;
- $this -> jiema = $jiema ;
- $this -> ztime = $ztime ;
- $this -> connect();
- }
-
- function connect(){
- $link=@mysql_connect($this->host,$this->name,$this->pass) or die ("连接服务器失败");
- @mysql_select_db($this->table,$link) or die("连接数据失败");
- @mysql_query("set names '$this->jiema'");
- @date_default_timezone_set("$this->ztime");
- }
-
- function query($sql) {
- if(!($query = @mysql_query($sql))) $this->show($sql);
- return $query;
- }
-
- function show($message = '', $sql = '') {
- if(!$sql) echo $message;
- else echo $message.'<br>'.$sql;
- }
-
- function result($query,$row,$values) {
- return @mysql_result($query,$row,$values);
- }
-
- function get_values($table,$row,$values) {
- $query = $this -> query("select * from $table");
- $returnvalues = mysql_result($query,$row,$values);
- return $returnvalues;
- }
-
- function num_rows($query) {
- return @mysql_num_rows($query);
- }
-
- function fetch($query) {
- return @mysql_fetch_array($query);
- }
-
- function insert_id() {
- return mysql_insert_id();
- }
-
- function fetch_row($query) {
- return mysql_fetch_row($query);
- }
-
- function fn_insert($table,$name,$value){
- if($this->query("insert into $table ($name) values ($value)")){
- return true;
- }else{
- return false;
- }
- }
-
- function sql_insert($tbname,$postvalues){
- foreach ($postvalues as $key => $value) {
- $postvalue .= "`".$key."`".",";
- $sqlvalue .= "'".$value."',";
- }
- $sqlfield = mb_substr("$postvalue",0,-1,'gbk');
- $sqlvalue = mb_substr("$sqlvalue",0,-1,'gbk');
- if($this-> fn_insert("$tbname","$sqlfield","$sqlvalue")){
- return true;
- }else{
- return false;
- }
- }
-
- function sql_update($table,$postvalues,$wwhere){
- foreach ($postvalues as $key=>$value) {
- $sqlfield .= $key."="."'".$value."'".",";
- }
- $sqlfield= mb_substr("$sqlfield",0,-1,'gbk');
- if($this->fn_update("$table","$sqlfield","$wwhere")){
- return true;
- }else{
- return false;
- }
- }
-
- function fn_update($table,$value,$wwhere){
- if($this->query("update $table set $value where $wwhere")){
- return true;
- }else{
- return false;
- }
- }
-
- function sql_delete($table,$wwhere){
- if($this->query("delete from $table where $wwhere")){
- return true;
- }else{
- return false;
- }
- }
-
- function close() {
- return mysql_close();
- }
- }
- $db = new mysql($location['host'],$location['hostname'],$location['hostpass'],$location['table'],$location['jiema']
|