Mysql.class.php

來源:互聯網
上載者:User

標籤:學院   iss   串連   ram   返回   config   als   關閉   prot   

<?php
/**
*================================================================
* Mysql.class.php 資料庫操作類,負責資料庫的底層操作
* @author 王超平
* @copyright 傳智播客PHP學院 2006-2013
* @version 1.0
* 2013年3月24日18:30:40
*================================================================
*/

class Mysql{
protected $conn = false; //資料庫連接資源
protected $sql; //sql語句

/**
* 建構函式,負責串連伺服器、選擇資料庫、設定字元集等
* @param $config string 配置數組
*/
public function __construct($config = array()){
$host = isset($config[‘host‘])? $config[‘host‘] : ‘localhost‘;
$user = isset($config[‘user‘])? $config[‘user‘] : ‘root‘;
$password = isset($config[‘password‘])? $config[‘password‘] : ‘‘;
$dbname = isset($config[‘dbname‘])? $config[‘dbname‘] : ‘‘;
$port = isset($config[‘port‘])? $config[‘port‘] : ‘3306‘;
$charset = isset($config[‘charset‘])? $config[‘charset‘] : ‘3306‘;

$this->conn = mysql_connect("$host:$port",$user,$password) or die(‘資料庫連接錯誤‘);
mysql_select_db($dbname) or die(‘資料庫選擇錯誤‘);
$this->setChar($charset);
}

/**
* 設定字元集
* @access private
* @param $charset string 字元集
*/
private function setChar($charest){
$sql = ‘set names ‘.$charest;
$this->query($sql);
}

/**
* 執行sql語句
* @access public
* @param $sql string 查詢sql語句
* @return $result,成功返回資源,失敗則輸出錯誤資訊,並退出
*/
public function query($sql){
//還可以加一個開關來開啟/關閉 sql日誌
//以追加的方式來儲存
$temp = "[" . date(‘Y-m-d H:i:s‘) ."] " . $sql . PHP_EOL;
file_put_contents("log.txt", $temp,FILE_APPEND);

$this->sql = $sql;
$result = mysql_query($this->sql,$this->conn);

if (! $result) {
die($this->errno().‘:‘.$this->error().‘<br />出錯語句為‘.$this->sql.‘<br />‘);
}
return $result;
}

/**
* 擷取第一條記錄的第一個欄位
* @access public
* @param $sql string 查詢的sql語句
* @return 返回一個該欄位的值
*/
public function getOne($sql){
$result = $this->query($sql);
$row = mysql_fetch_row($result);
if ($row) {
return $row[0];
} else {
return false;
}
}

/**
* 擷取一條記錄
* @access public
* @param $SQL 查詢的sql語句
* @return array 關聯陣列
*/
public function getRow($sql){
if ($result = $this->query($sql)) {
$row = mysql_fetch_assoc($result);
return $row;
} else {
return false;
}
}

/**
* 擷取所有的記錄
* @access public
* @param $sql 執行的sql語句
* @return $list 有所有記錄組成的二維數組
*/
public function getAll($sql){
$result = $this->query($sql);
$list = array();
while ($row = mysql_fetch_assoc($result)){
$list[] = $row;
}
return $list;
}

/**
* 擷取某一列的值
* @access public
* @param $sql string 執行的sql語句
* @return $list array 返回由該列的值構成的一維數組
*/
public function getCol($sql){
$result = $this->query($sql);
$list = array();
while ($row = mysql_fetch_row($result)) {
$list[] = $row[0];
}
return $list;
}


/**
* 擷取上一步insert操作產生的id
*/
public function getInsertId(){
return mysql_insert_id($this->conn);
}
/**
* 擷取錯誤號碼
* @access private
* @return 錯誤號碼
*/
public function errno(){
return mysql_errno($this->conn);
}

/**
* 擷取錯誤資訊
* @access private
* @return 錯誤private資訊
*/
public function error(){
return mysql_error($this->conn);
}

}

Mysql.class.php

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.