日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

PHP和MySQL

發(fā)布時間:2025/3/20 数据库 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 PHP和MySQL 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

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é)

以上是生活随笔為你收集整理的PHP和MySQL的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。