PHP和MySQL
PHP和MySQL被稱為黃金搭檔,幾乎所有的基于PHP應用的項目都在使用MySQL,在PHP中,連接MySQL數(shù)據(jù)庫十分簡單,簡單到只需要一個函數(shù)函數(shù)即可:
mysql_connect($host,$username,$password)
它有三個參數(shù),分別是數(shù)據(jù)庫主機名,數(shù)據(jù)庫用戶名,數(shù)據(jù)庫用戶密碼。
如果我們的數(shù)據(jù)庫在本地,那么數(shù)據(jù)庫主機名可寫為127.0.0.1。例如,我們可以使用這個方法連接數(shù)據(jù)庫:
$con=mysql_connect('127.0.0.1','user','123456') or die("服務器連接失敗!"); ?
mysql_select_db('test',$con); ?
mysql_query("set names 'gb2312'"); ?
三行代碼就實現(xiàn)了連接MySQL數(shù)據(jù)庫。在上面例子中,mysql_select_db()表示選擇數(shù)據(jù)庫,上例表示連接test數(shù)據(jù)庫,其中set names 'gb2312'表示設置數(shù)據(jù)庫讀取的編碼為gb2312。
如果數(shù)據(jù)庫的用戶名和密碼錯誤,頁面上自動會提示“服務器連接失敗”。
一般的,我們在做OOP的開發(fā)當中,習慣上把PHP連接數(shù)據(jù)庫的部分,寫在一個單獨的類中。例如:
class cls_mysql ?
{ ?
protected $link_id; ?
? ?public function __construct($dbhost, $dbuser, $dbpw, $dbname = '', $charset = 'utf8')//構(gòu)造函數(shù) ?
? ?{ ?
? ? ? ?if(!($this->link_id = mysql_connect($dbhost, $dbuser, $dbpw))) ?
? ? ? ?{ ?
? ? ? ? ? ?$this->ErrorMsg("Can't pConnect MySQL Server($dbhost)!"); ?
? ? ? ?} ?
? ? ? ?mysql_query("SET NAMES " . $charset, $this->link_id);//設置編碼 ?
? ? ? ?if ($dbname) ?
? ? ? ?{ ?
? ? ? ? ? ?if (mysql_select_db($dbname, $this->link_id) === false ) ?
? ? ? ? ? ?{ ?
? ? ? ? ? ? ? ?$this->ErrorMsg("Can't select MySQL database($dbname)!"); ?
? ? ? ? ? ? ? ?return false; ?
? ? ? ? ? ?} ?
? ? ? ? ? ?else ?
? ? ? ? ? ?{ ?
? ? ? ? ? ? ? ?return true; ?
? ? ? ? ? ?} ?
? ? ? ?} ?
? ?} ?
? ?public function select_database($dbname)//選擇數(shù)據(jù)庫 ?
? ?{ ?
? ? ? ?return mysql_select_db($dbname, $this->link_id); ?
? ?} ?
? ?public function fetch_array($query, $result_type = MYSQL_ASSOC)//得到遍歷后的數(shù)據(jù),是一個數(shù)組形式 ?
? ?{ ?
? ? ? ?return mysql_fetch_array($query, $result_type); ?
? ?} ?
? ?public function query($sql)//執(zhí)行查詢 ?
? ?{ ?
? ? ? ?return mysql_query($sql, $this->link_id); ?
? ?} ?
? ?public function affected_rows()//得到影響的記錄集數(shù) ?
? ?{ ?
? ? ? ?return mysql_affected_rows($this->link_id); ?
? ?} ?
? ?public function num_rows($query)//獲得查詢的記錄數(shù) ?
? ?{ ?
? ? ? ?return mysql_num_rows($query); ?
? ?} ?
? ?public function insert_id() ?
? ?{ ?
? ? ? ?return mysql_insert_id($this->link_id);//獲得插入的id ?
? ?} ?
? ?public function selectLimit($sql, $num, $start = 0) ?
? ?{ ?
? ? ? ?if ($start == 0) ?
? ? ? ?{ ?
? ? ? ? ? ?$sql .= ' LIMIT ' . $num; ?
? ? ? ?} ?
? ? ? ?else ?
? ? ? ?{ ?
? ? ? ? ? ?$sql .= ' LIMIT ' . $start . ', ' . $num; ?
? ? ? ?} ?
? ? ? ?return $this->query($sql); ?
? ?} ?
? ?public function getOne($sql, $limited = false)//獲取一條記錄 ?
? ?{ ?
? ? ? ?if ($limited == true) ?
? ? ? ?{ ?
? ? ? ? ? ?$sql = trim($sql . ' LIMIT 1'); ?
? ? ? ?} ?
? ? ? ?$res = $this->query($sql); ?
? ? ? ?if ($res !== false) ?
? ? ? ?{ ?
? ? ? ? ? ?$row = mysql_fetch_row($res); ?
? ? ? ? ? ?return $row[0]; ?
? ? ? ?} ?
? ? ? ?else ?
? ? ? ?{ ?
? ? ? ? ? ?return false; ?
? ? ? ?} ?
? ?} ?
? ?public function getrow($sql) ?
? ?{ ?
? ? ? ?$res = $this->query($sql); ?
? ? ? ?if ($res !== false) ?
? ? ? ?{ ?
? ? ? ? ? ?return mysql_fetch_assoc($res); ?
? ? ? ?} ?
? ? ? ?else ?
? ? ? ?{ ?
? ? ? ? ? ?return false; ?
? ? ? ?} ?
? ?} ?
? ?public function getAll($sql) ?
? ?{ ?
? ? ? ?$res = $this->query($sql); ?
? ? ? ?if ($res !== false) ?
? ? ? ?{ ?
? ? ? ? ? ?$arr = array(); ?
? ? ? ? ? ?while ($row = mysql_fetch_assoc($res)) ?
? ? ? ? ? ?{ ?
? ? ? ? ? ? ? ?$arr[] = $row; ?
? ? ? ? ? ?} ?
? ? ? ? ? ?return $arr; ?
? ? ? ?} ?
? ? ? ?else ?
? ? ? ?{ ?
? ? ? ? ? ?return false; ?
? ? ? ?} ?
? ?} ?
? ?function ErrorMsg($message = '', $sql = '') ?
? ?{ ?
? ? ? ?if ($message) ?
? ? ? ?{ ?
? ? ? ? ? ?echo "<b>error info</b>: $message\n\n"; ?
? ? ? ?} ?
? ? ? ?else ?
? ? ? ?{ ?
? ? ? ? ? ?echo "<b>MySQL server error report:"; ?
? ? ? ? ? ?print_r($this->error_message); ?
? ? ? ?} ?
? ? ? ?exit; ?
? ?} ? ? ?
} ?
轉(zhuǎn)載于:https://blog.51cto.com/avery2013/1365966
總結(jié)
- 上一篇: Learning Cocos2d-x f
- 下一篇: linux cmake编译源码,linu