《LINUX入門:PHP數(shù)據(jù)庫操作Helper類完整實(shí)例》要點(diǎn):
本文介紹了LINUX入門:PHP數(shù)據(jù)庫操作Helper類完整實(shí)例,希望對(duì)您有用。如果有疑問,可以聯(lián)系我們。
本文實(shí)例講述了PHP數(shù)據(jù)庫操作Helper類.分享給大家供大家參考,具體如下:
php操作數(shù)據(jù)庫分為幾個(gè)步驟(這里以MYSQL為例):
1. 建立連接
$connection=mysql_connect($db_host,$db_username,$db_password);
2. 選擇數(shù)據(jù)庫
$db_select=mysql_select_db($db_database);
3. 執(zhí)行CRUD操作
mysql_query("set names 'utf8'");//編碼 $result=mysql_query($sqlstring);
(mysql_affected_rows()前一次mysql操作所影響的記錄行數(shù))
4. 查詢
mysql_fetch_array($result); mysql_fetch_row($result);
5. 關(guān)閉連接
mysql_close($connection);
DBHelper.php類文件:
<?php class DBHelper { //建立連接 function GetConnection($db_host,$db_username,$db_password) { $connection=mysql_connect($db_host,$db_username,$db_password); if($connection==false) die("數(shù)據(jù)庫連接失敗:".mysql_error());//輸入具體錯(cuò)誤信息 return $connection; } //選擇對(duì)應(yīng)數(shù)據(jù)庫 function DBSelect($db_database) { $db_select=mysql_select_db($db_database); if($db_select==false) die("數(shù)據(jù)庫選擇失?。?.mysql_error()); return $db_select; } //執(zhí)行CRUD操作 function Excute($sqlstring) { $result=mysql_query($sqlstring); return $result; } //釋放資源 function CloseConnection($connection) { if($connection!=null) mysql_close($connection); } } ?>
dbtext.php配置文件:
<?php $db_host="localhost"; $db_database="mymessage"; $db_username="root"; $db_password="123456"; ?>
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP基于pdo操作數(shù)據(jù)庫技巧總結(jié)》、《PHP+MongoDB數(shù)據(jù)庫操作技巧大全》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所贊助.
轉(zhuǎn)載請(qǐng)注明本頁網(wǎng)址:
http://www.snjht.com/jiaocheng/6720.html