- * 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);
- }
- //最后一次插入紀錄的id值
- 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();
- }//開源代碼Vevb.com
- }
- $db = new mysql($location['host'],$location['hostname'],$location['hostpass'],$location['table'],$location['jiema']
新聞熱點
疑難解答