本身這就是一個自定義函數(shù),數(shù)據(jù)需要插入的話需要自己構(gòu)造sql語句然后通過mysql_query將函數(shù)返回的值寫入數(shù)據(jù)庫。

創(chuàng)新互聯(lián)公司主要從事做網(wǎng)站、網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)孟州,10余年網(wǎng)站建設(shè)經(jīng)驗,價格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):028-86922220
想直接讓數(shù)據(jù)庫調(diào)用PHP的自定義函數(shù)是不現(xiàn)實的!
PHP使用VB封裝成DLL?沒有試過。
不過,Zend Corporation ()
開發(fā)的產(chǎn)品 Zend Studio 可以裝PHP進(jìn)行編譯,編譯過的PHP運(yùn)行速度比不編譯的要快。而且,因為編譯后生成的是二進(jìn)制文件,所以,Zend Studio 也就達(dá)到了為PHP加密的功能(雖然國內(nèi)已經(jīng)有人開發(fā)了一種能對被Zend Studio某些版本編譯過的PHP進(jìn)行反編譯,從而得到源代碼)。Zend Studio并不是免費(fèi)的。
經(jīng)過編譯后的PHP程序不能夠再運(yùn)行,你的服務(wù)器上必須裝有Zend 公司的另一免費(fèi)產(chǎn)品:Zend Optimizer。使用Zend Optimizer 后,就可以在你的服務(wù)器上正常運(yùn)行編譯過的PHP程序了。
php封裝mysql類
復(fù)制代碼
代碼如下:
?php
class
Mysql
{
private
$host;
private
$user;
private
$pwd;
private
$dbName;
private
$charset;
private
$conn
=
null;
public
function
__construct()
{
$this-host
=
'localhost';
$this-user
=
'root';
$this-pwd
=
'root';
$this-dbName
=
'test';
$this-connect($this-host,$this-user,$this-pwd);
$this-switchDb($this-dbName);
$this-setChar($this-charset);
}
//負(fù)責(zé)鏈接
private
function
connect($h,$u,$p)
{
$conn
=
mysql_connect($h,$u,$p);
$this-conn
=
$conn;
}
//負(fù)責(zé)切換數(shù)據(jù)庫
public
function
switchDb($db)
{
$sql
=
'use'
.
$db;
$this-query($sql);
}
//負(fù)責(zé)設(shè)置字符集
public
function
setChar($char)
{
$sql
=
'set
names'
.
$char;
$this-query($sql);
}
//負(fù)責(zé)發(fā)送sql查詢
public
function
query($sql)
{
return
mysql_query($sql,$this-conn);
}
//負(fù)責(zé)獲取多行多列的select結(jié)果
public
function
getAll($sql)
{
$list
=
array();
$rs
=
$this-query($sql);
if
(!$rs)
{
return
false;
}
while
($row
=
mysql_fetch_assoc($rs))
{
$list[]
=
$row;
}
return
$list;
}
public
function
getRow($sql)
{
$rs
=
$this-query($sql);
if(!$rs)
{
return
false;
}
return
mysql_fetch_assoc($rs);
}
public
function
getOne($sql)
{
$rs
=
$this-query($sql);
if
(!$rs)
{
return
false;
}
return
mysql_fetch_assoc($rs);
return
$row[0];
}
public
function
close()
{
mysql_close($this-conn);
}
}
echo
'pre';
$mysql
=
new
Mysql();
print_r($mysql);
$sql
=
"insert
into
stu
values
(4,'wangwu','99998')";
if($mysql-query($sql)){
echo
"query成功";
}else
{
echo
"失敗";
}
echo
"br
/";
$sql
=
"select
*
from
stu";
$arr
=
$mysql-getAll($sql);
print_r($arr);
?
?php
class dbname{
private $localhost;
private $root;
private $pass;
private $db_name;
public function __construct($localhost,$root,$pass,$db_name){
$this-localhost=$localhost;
$this-root=$root;
$this-pass=$pass;
$this-db_name=$db_name;
$this-query();
}
private function query(){
mysql_connect($this-localhost,$this-root,$this-pass);
mysql_select_db($this-db_name);
mysql_query("set names utf8");
}
}
?
?php
class MySQL{
private $host; //服務(wù)器地址
private $name; //登錄賬號
private $pwd; //登錄密碼
private $dBase; //數(shù)據(jù)庫名稱
private $conn; //數(shù)據(jù)庫鏈接資源
private $result; //結(jié)果集
private $msg; //返回結(jié)果
private $fields; //返回字段
private $fieldsNum; //返回字段數(shù)
private $rowsNum; //返回結(jié)果數(shù)
private $rowsRst; //返回單條記錄的字段數(shù)組
private $filesArray = array(); //返回字段數(shù)組
private $rowsArray = array(); //返回結(jié)果數(shù)組
private $charset='utf8'; //設(shè)置操作的字符集
private $query_count=0; //查詢結(jié)果次數(shù)
static private $_instance; //存儲對象
//初始化類
private function __construct($host='',$name='',$pwd='',$dBase=''){
if($host != '') $this-host = $host;
if($name != '') $this-name = $name;
if($pwd != '') $this-pwd = $pwd;
if($dBase != '') $this-dBase = $dBase;
$this-init_conn();
}
//防止被克隆
private function __clone(){}
public static function getInstance($host='',$name='',$pwd='',$dBase=''){
if(FALSE == (self::$_instance instanceof self)){
self::$_instance = new self($host,$name,$pwd,$dBase);
}
return self::$_instance;
}
public function __set($name,$value){
$this-$name=$value;
}
public function __get($name){
return $this-$name;
}
//鏈接數(shù)據(jù)庫
function init_conn(){
$this-conn=@mysql_connect($this-host,$this-name,$this-pwd) or die('connect db fail !');
@mysql_select_db($this-dBase,$this-conn) or die('select db fail !');
mysql_query("set names ".$this-charset);
}
//查詢結(jié)果
function mysql_query_rst($sql){
if($this-conn == '') $this-init_conn();
$this-result = @mysql_query($sql,$this-conn);
$this-query_count++;
}
//取得字段數(shù)
function getFieldsNum($sql){
$this-mysql_query_rst($sql);
$this-fieldsNum = @mysql_num_fields($this-result);
}
//取得查詢結(jié)果數(shù)
function getRowsNum($sql){
$this-mysql_query_rst($sql);
if(mysql_errno() == 0){
return @mysql_num_rows($this-result);
}else{
return '';
}
}
//取得記錄數(shù)組(單條記錄)
function getRowsRst($sql,$type=MYSQL_BOTH){
$this-mysql_query_rst($sql);
if(empty($this-result)) return '';
if(mysql_error() == 0){
$this-rowsRst = mysql_fetch_array($this-result,$type);
return $this-rowsRst;
}else{
return '';
}
}
//取得記錄數(shù)組(多條記錄)
function getRowsArray($sql,$type=MYSQL_BOTH){
!empty($this-rowsArray) ? $this-rowsArray=array() : '';
$this-mysql_query_rst($sql);
if(mysql_errno() == 0){
while($row = mysql_fetch_array($this-result,$type)) {
$this-rowsArray[] = $row;
}
return $this-rowsArray;
}else{
return '';
}
}
//更新、刪除、添加記錄數(shù)
function uidRst($sql){
if($this-conn == ''){
$this-init_conn();
}
@mysql_query($sql);
$this-rowsNum = @mysql_affected_rows();
if(mysql_errno() == 0){
return $this-rowsNum;
}else{
return '';
}
}
//返回最近插入的一條數(shù)據(jù)庫的id值
function returnRstId($sql){
if($this-conn == ''){
$this-init_conn();
}
@mysql_query($sql);
if(mysql_errno() == 0){
return mysql_insert_id();
}else{
return '';
}
}
//獲取對應(yīng)的字段值
function getFields($sql,$fields){
$this-mysql_query_rst($sql);
if(mysql_errno() == 0){
if(mysql_num_rows($this-result) 0){
$tmpfld = @mysql_fetch_row($this-result);
$this-fields = $tmpfld[$fields];
}
return $this-fields;
}else{
return '';
}
}
//錯誤信息
function msg_error(){
if(mysql_errno() != 0) {
$this-msg = mysql_error();
}
return $this-msg;
}
//釋放結(jié)果集
function close_rst(){
mysql_free_result($this-result);
$this-msg = '';
$this-fieldsNum = 0;
$this-rowsNum = 0;
$this-filesArray = '';
$this-rowsArray = '';
}
//關(guān)閉數(shù)據(jù)庫
function close_conn(){
$this-close_rst();
mysql_close($this-conn);
$this-conn = '';
}
//取得數(shù)據(jù)庫版本
function db_version() {
return mysql_get_server_info();
}
}
ThinkPHP內(nèi)置了抽象數(shù)據(jù)庫訪問層,把不同的數(shù)據(jù)庫操作封裝起來,我們只需要使用公共的Db類進(jìn)行操作,而無需針對不同的數(shù)據(jù)庫寫不同的代碼和底層實現(xiàn),Db類會自動調(diào)用相應(yīng)的數(shù)據(jù)庫驅(qū)動來處理。目前的數(shù)據(jù)庫包括Mysql、SqlServer、PgSQL、Sqlite、Oracle、Ibase、Mongo,也包括對PDO的支持,如果應(yīng)用需要使用數(shù)據(jù)庫,必須配置數(shù)據(jù)庫連接信息,數(shù)據(jù)庫的配置文件有多種定義方式。
常用的配置方式是在項目配置文件中添加下面的參數(shù):
?php
//項目配置文件
return array(
//數(shù)據(jù)庫配置信息
'DB_TYPE' = 'mysql', // 數(shù)據(jù)庫類型
'DB_HOST' = 'localhost', // 服務(wù)器地址
'DB_NAME' = 'thinkphp', // 數(shù)據(jù)庫名
'DB_USER' = 'root', // 用戶名
'DB_PWD' = '', // 密碼
'DB_PORT' = 3306, // 端口
'DB_PREFIX' = 'think_', // 數(shù)據(jù)庫表前綴
//其他項目配置參數(shù)
// ...
);
或者采用如下配置
'DB_DSN' = 'mysql://username:password@localhost:3306/DbName'
使用DB_DSN方式定義可以簡化配置參數(shù),DSN參數(shù)格式為:
數(shù)據(jù)庫類型://用戶名:密碼@數(shù)據(jù)庫地址:數(shù)據(jù)庫端口/數(shù)據(jù)庫名
如果兩種配置參數(shù)同時存在的話,DB_DSN配置參數(shù)優(yōu)先。
注意:如果要設(shè)置分布式數(shù)據(jù)庫,暫時不支持DB_DSN方式配置。1
如果采用PDO驅(qū)動的話,則必須首先配置DB_TYPE 為pdo,然后還需要單獨配置其他參數(shù),例如:
//PDO連接方式
'DB_TYPE' = 'pdo', // 數(shù)據(jù)庫類型
'DB_USER' = 'root', // 用戶名
'DB_PWD' = '', // 密碼
'DB_PREFIX' = 'think_', // 數(shù)據(jù)庫表前綴
'DB_DSN' = 'mysql:host=localhost;dbname=thinkphp;charset=UTF-8'
注意:PDO方式的DB_DSN配置格式有所區(qū)別,根據(jù)不同的數(shù)據(jù)庫類型設(shè)置有所不同。
配置文件定義的數(shù)據(jù)庫連接信息一般是系統(tǒng)默認(rèn)采用的,因為一般一個項目的數(shù)據(jù)庫訪問配置是相同的。該方法系統(tǒng)在連接數(shù)據(jù)庫的時候會自動獲取,無需手動連接。
可以對每個項目和不同的分組定義不同的數(shù)據(jù)庫連接信息,如果開啟了調(diào)試模式的話,還可以在不同的應(yīng)用狀態(tài)的配置文件里面定義獨立的數(shù)據(jù)庫配置信息。1
第二種 在模型類里面定義connection屬性
如果在某個模型類里面定義了connection屬性的話,則實例化該自定義模型的時候會采用定義的數(shù)據(jù)庫連接信息,而不是配置文件中設(shè)置的默認(rèn)連接信息,通常用于某些數(shù)據(jù)表位于當(dāng)前數(shù)據(jù)庫連接之外的其它數(shù)據(jù)庫,例如:
//在模型里單獨設(shè)置數(shù)據(jù)庫連接信息
protected $connection = array(
'db_type' = 'mysql',
'db_user' = 'root',
'db_pwd' = '1234',
'db_host' = 'localhost',
'db_port' = '3306',
'db_name' = 'thinkphp'
);
也可以采用DSN方式定義,例如:
//或者使用DSN定義
protected $connection = 'mysql://root:1234@localhost:3306/thinkphp';
如果我們已經(jīng)在配置文件中配置了額外的數(shù)據(jù)庫連接信息,例如:
//數(shù)據(jù)庫配置1
'DB_CONFIG1' = array(
'db_type' = 'mysql',
'db_user' = 'root',
'db_pwd' = '1234',
'db_host' = 'localhost',
'db_port' = '3306',
'db_name' = 'thinkphp'
),
//數(shù)據(jù)庫配置2
'DB_CONFIG2' = 'mysql://root:1234@localhost:3306/thinkphp';
那么,我們可以把模型類的屬性定義改為:
//調(diào)用配置文件中的數(shù)據(jù)庫配置1
protected $connection = 'DB_CONFIG1';
//調(diào)用配置文件中的數(shù)據(jù)庫配置2
protected $connection = 'DB_CONFIG2';
如果采用的是M方法實例化模型的話,也可以支持傳入不同的數(shù)據(jù)庫連接信息,例如:
$User = M('User','other_','mysql://root:1234@localhost/demo');
表示實例化User模型,連接的是demo數(shù)據(jù)庫的other_user表,采用的連接信息是第三個參數(shù)配置的。如果我們在項目配置文件中已經(jīng)配置了DB_CONFIG2的話,也可以采用:
$User = M('User','other_','DB_CONFIG2');
如果你的個別數(shù)據(jù)表沒有定義任何前綴的話,可以在前綴參數(shù)中傳入NULL,例如:
$User = M('User',Null,'DB_CONFIG2');
表示實例化User模型,連接的是demo數(shù)據(jù)庫的user表。
需要注意的是,ThinkPHP的數(shù)據(jù)庫連接的惰性的,所以并不是在實例化的時候就連接數(shù)據(jù)庫,而是在有實際的數(shù)據(jù)操作的時候才會去連接數(shù)據(jù)庫(額外的情況是,在系統(tǒng)第一次實例化模型的時候,會自動連接數(shù)據(jù)庫獲取相關(guān)模型類對應(yīng)的數(shù)據(jù)表的字段信息)。
分享文章:php怎么封裝數(shù)據(jù)庫文件,PHP封裝
文章URL:http://chinadenli.net/article24/dsgdgje.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信小程序、定制網(wǎng)站、軟件開發(fā)、用戶體驗、標(biāo)簽優(yōu)化、網(wǎng)站導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)