PHP數(shù)據(jù)庫操作Helper類完整實例
本文實例講述了PHP數(shù)據(jù)庫操作Helper類。分享給大家供大家參考,具體如下:
php操作數(shù)據(jù)庫分為幾個步驟(這里以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. 關閉連接
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());//輸入具體錯誤信息
return $connection;
}
//選擇對應數(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"; ?>
更多關于PHP相關內(nèi)容感興趣的讀者可查看本站專題:《PHP基于pdo操作數(shù)據(jù)庫技巧總結》、《PHP+MongoDB數(shù)據(jù)庫操作技巧大全》、《php面向對象程序設計入門教程》、《php字符串(string)用法總結》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設計有所幫助。
- PHP數(shù)據(jù)庫表操作的封裝類及用法實例詳解
- PHP訪問MYSQL數(shù)據(jù)庫封裝類(附函數(shù)說明)
- php數(shù)據(jù)庫操作model類(使用__call方法)
- php實現(xiàn)的簡單數(shù)據(jù)庫操作Model類
- php mysql數(shù)據(jù)庫操作類
- 簡單的php數(shù)據(jù)庫操作類代碼(增,刪,改,查)
- php實現(xiàn)mysql數(shù)據(jù)庫備份類
- 全新的PDO數(shù)據(jù)庫操作類php版(僅適用Mysql)
- PHP操作XML作為數(shù)據(jù)庫的類
- PHP+Mysql樹型結構(無限分類)數(shù)據(jù)庫設計的2種方式實例
- PHP數(shù)據(jù)庫處理封裝類實例
相關文章
學習php設計模式 php實現(xiàn)策略模式(strategy)
這篇文章主要介紹了php設計模式中的適配器模式,使用php實現(xiàn)適配器模式,感興趣的小伙伴們可以參考一下2015-12-12

