本文實例講述了PHP利用pdo_odbc實現(xiàn)連接數(shù)據(jù)庫。分享給大家供大家參考,具體如下:
創(chuàng)新互聯(lián)是一家專注于網(wǎng)站設計、做網(wǎng)站與策劃設計,碭山網(wǎng)站建設哪家好?創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設10多年,網(wǎng)設計領(lǐng)域的專業(yè)建站公司;建站業(yè)務涵蓋:碭山等地區(qū)。碭山做網(wǎng)站價格咨詢:18982081108
目的:從sql
server數(shù)據(jù)庫里面把某個視圖文件調(diào)用出來,以鍵值對的方式顯示在頁面上。
利用pdo
odbc來實現(xiàn)PHP連接數(shù)據(jù)庫:
在PHP配置文件里面開啟pdo_odbc.dll服務。重啟Apache服務器。
在ThinkPHP5.1的項目中在模塊里添加config添加規(guī)定好的樣式數(shù)據(jù)庫:
代碼如下:
?php
return
[
//
數(shù)據(jù)庫類型
'type'
=
'sqlsrv',
//
服務器地址
'hostname'
=
'localhost',
//
數(shù)據(jù)庫名
'database'
=
'mysql',
//
用戶名
'username'
=
'sa',
//
密碼
'password'
=
'123456',
//
端口
'hostport'
=
'',
//
連接dsn
'dsn'
=
'odbc:Driver={SQL
Server};Server=localhost;Database=mysql',
//
數(shù)據(jù)庫連接參數(shù)
'params'
=
[],
//
數(shù)據(jù)庫編碼默認采用utf8
'charset'
=
'utf8',
//
數(shù)據(jù)庫表前綴
'prefix'
=
'',
//
數(shù)據(jù)庫調(diào)試模式
'debug'
=
true,
//
數(shù)據(jù)庫部署方式:0
集中式(單一服務器),1
分布式(主從服務器)
'deploy'
=
0,
//
數(shù)據(jù)庫讀寫是否分離
主從式有效
'rw_separate'
=
false,
//
讀寫分離后
主服務器數(shù)量
'master_num'
=
1,
//
指定從服務器序號
'slave_no'
=
'',
//
是否嚴格檢查字段是否存在
'fields_strict'
=
true,
//
數(shù)據(jù)集返回類型
'resultset_type'
=
'array',
//
自動寫入時間戳字段
'auto_timestamp'
=
false,
//
時間字段取出后的默認時間格式
'datetime_format'
=
'Y-m-d
H:i:s',
//
是否需要進行SQL性能分析
'sql_explain'
=
false,
//
Builder類
'builder'
=
'',
//
Query類
'query'
=
'\\think\\db\\Query',
//
是否需要斷線重連
'break_reconnect'
=
false,
//
斷線標識字符串
'break_match_str'
=
[],
];
?
在控制器controller里面建一個控制文件Test.php
代碼如下:
?php
namespace
app\index\controller;
use
think\Db;
use
think\Controller;
class
Test
extends
Controller
{
public
function
zz(){
$data=Db::view('View_2')-select();
echo
json_encode($data);
}
}
?
最后調(diào)用入口文件即可訪問。
我的效果:
[{"111":"123","1112":"LLP","232":"1","ROW_NUMBER":"1"},{"111":"123","1112":"BB","232":"2","ROW_NUMBER":"2"}]
更多關(guān)于thinkPHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《ThinkPHP入門教程》、《thinkPHP模板操作技巧總結(jié)》、《ThinkPHP常用方法總結(jié)》、《codeigniter入門教程》、《CI(CodeIgniter)框架進階教程》、《Zend
FrameWork框架入門教程》及《PHP模板技術(shù)總結(jié)》。
希望本文所述對大家基于ThinkPHP框架的PHP程序設計有所幫助。
您可能感興趣的文章:ThinkPHP實現(xiàn)多數(shù)據(jù)庫連接的解決方法tp5(thinkPHP5)框架實現(xiàn)多數(shù)據(jù)庫查詢的方法ThinkPHP3.1新特性之多數(shù)據(jù)庫操作更加完善tp5(thinkPHP5)框架連接數(shù)據(jù)庫的方法示例PHP7使用ODBC連接SQL
Server2008
R2數(shù)據(jù)庫示例【基于thinkPHP5.1框架】thinkPHP5實現(xiàn)的查詢數(shù)據(jù)庫并返回json數(shù)據(jù)實例tp5(thinkPHP5)操作mongoDB數(shù)據(jù)庫的方法tp5(thinkPHP5)框架數(shù)據(jù)庫Db增刪改查常見操作總結(jié)thinkPHP5框架實現(xiàn)多數(shù)據(jù)庫連接,跨數(shù)據(jù)連接查詢操作示例
?php
$conn=mysql_connect("localhost","root","111111") or die("數(shù)據(jù)庫服務器連接錯誤".mysql_error());
mysql_select_db("db_bookshop",$conn) or die("數(shù)據(jù)庫訪問錯誤".mysql_error());
mysql_query("set names gb2312");
?
數(shù)據(jù)庫名、用戶名、密碼需要進行相應的修改。。
本文實例講述了PHP實現(xiàn)的pdo連接數(shù)據(jù)庫并插入數(shù)據(jù)功能。分享給大家供大家參考,具體如下:
創(chuàng)建配置文件
pdo_config.php
?php
$db_Type
=
"mysql";//數(shù)據(jù)庫類型
$host
=
"localhost";//主機名
$dbName
=
"test";//數(shù)據(jù)庫名
$userName
=
"root";//用戶名
$password
=
"root";//密碼
$dsn
=
"{$db_Type}:host={$host};dbname={$dbName}";
?
pdo插入數(shù)據(jù)庫
pdo_insert.php
?php
header('Content-type:text/html;
charset=utf-8');
require
'pdo_config.php';
try{
$pdo
=
new
PDO
($dsn,$userName,$password);//創(chuàng)建一個連接對象
$pdo-exec('set
names
utf8');//設置編碼
$sql
=
"INSERT
student
(name,email)
VALUES
('李四','123@qq.com')";
$pdo-exec($sql);
}catch
(PDOException
$e){
die('操作失敗'.$e-getMessage());
}
//關(guān)閉連接
$pdo
=
null;
?
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP基于pdo操作數(shù)據(jù)庫技巧總結(jié)》、《php+mysqli數(shù)據(jù)庫程序設計技巧總結(jié)》、《php面向?qū)ο蟪绦蛟O計入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設計有所幫助。
您可能感興趣的文章:關(guān)于php連接mssql:pdo
odbc
sql
serverPHP5中使用PDO連接數(shù)據(jù)庫的方法PHP中PDO連接數(shù)據(jù)庫中各種DNS設置方法小結(jié)ThinkPHP框架基于PDO方式連接數(shù)據(jù)庫操作示例PHP使用ODBC連接數(shù)據(jù)庫的方法tp5(thinkPHP5)框架連接數(shù)據(jù)庫的方法示例PHP7使用ODBC連接SQL
Server2008
R2數(shù)據(jù)庫示例【基于thinkPHP5.1框架】tp5(thinkPHP5)操作mongoDB數(shù)據(jù)庫的方法thinkPHP5實現(xiàn)數(shù)據(jù)庫添加內(nèi)容的方法tp5(thinkPHP5)框架數(shù)據(jù)庫Db增刪改查常見操作總結(jié)PHP利用pdo_odbc實現(xiàn)連接數(shù)據(jù)庫示例【基于ThinkPHP5.1搭建的項目】
首先搭建PHP開發(fā)運行環(huán)境,安裝完成后再使用PHP連接mysql,代碼操作步驟如下:
下載php_mysql.dll擴展,放到ext文件夾下,如果存在則跳過;
打開php.ini配置文件,去掉extension=php_mysql.dll項前面的分號,如果已取掉則跳過此步驟;
然后運行phpinfo();如果存在mysql項,則說明已經(jīng)開啟mysql擴展。
php連接mysql數(shù)據(jù)庫操作:
運行結(jié)果:
常規(guī)方式
常規(guī)方式就是按部就班的讀取文件了。其余的話和上述方案一致。
// 讀取配置文件內(nèi)容
$handle = fopen("filepath", "r"); ? ? ? ? ? ?$content = fread($handle, filesize("filepath"));123
PHP解析XML
上述兩種讀取文件,其實都是為了PHP解析XML來做準備的。關(guān)于PHP解析XML的方式的博客有很多。方式也有很多,像simplexml,XMLReader,DOM啦等等。但是對于比較小型的xml配置文件,simplexml就足夠了。
配置文件
?xml version="1.0" encoding="UTF-8" ?mysql
!-- 為防止出現(xiàn)意外,請按照此標準順序書寫.其實也無所謂了 --
hostlocalhost/host
userroot/user
password123456/password
dbtest/db
port3306/port/mysql12345678910
解析
?php/**
* 作為解析XML配置文件必備工具
*/class XMLUtil {
public static $dbconfigpath = "./db.config.xml"; ? ?public static function getDBConfiguration() {
$dbconfig = array (); ? ? ? ?try { ? ? ? ? ? ?// 讀取配置文件內(nèi)容
$handle = fopen(self::$dbconfigpath, "r"); ? ? ? ? ? ?$content = fread($handle, filesize(self::$dbconfigpath)); ? ? ? ? ? ?// 獲取xml文檔根節(jié)點,進而獲取相關(guān)的數(shù)據(jù)庫信息
$mysql = simplexml_load_string($content); ? ? ? ? ? ?// 將獲取到的xml節(jié)點信息賦值給關(guān)聯(lián)數(shù)組,方便接下來的方法調(diào)用
$dbconfig['host'] = $mysql-host; ? ? ? ? ? ?$dbconfig['user'] = $mysql-user; ? ? ? ? ? ?$dbconfig['password'] = $mysql-password; ? ? ? ? ? ?$dbconfig['db'] = $mysql-db; ? ? ? ? ? ?$dbconfig['port'] = $mysql-port; ? ? ? ? ? ?// 將配置信息以關(guān)聯(lián)數(shù)組的形式返回
return $dbconfig;
} catch ( Exception $e ) { ? ? ? ? ? ?throw new RuntimeException ( "mark讀取數(shù)據(jù)庫配置文件信息出錯!/markbr /" );
} ? ? ? ?return $dbconfig;
}
}1234567891011121314151617181920212223242526272829
數(shù)據(jù)庫連接池
對于PHP程序而言,優(yōu)化永無止境。而數(shù)據(jù)庫連接池就在一定程度上起到了優(yōu)化的作用。其使得對用戶的每一個請求而言,無需每次都像數(shù)據(jù)庫申請鏈接資源。而是通過已存在的數(shù)據(jù)庫連接池中的鏈接來返回,從時間上,效率上,都是一個大大的提升。
于是,這里簡單的模擬了一下數(shù)據(jù)庫連接池的實現(xiàn)。核心在于維護一個“池”。
從池子中取,用畢,歸還給池子。
?php/**x
* ?PHP中的數(shù)據(jù)庫 工具類設計
* ?郭璞
* ?2016年12月23日
*
**/class DbHelper { ? ?private $dbconfig; ? ?private $dbpool; ? ?public $poolsize; ? ?public function __construct($poolsize = 20) { ? ? ? ?if (! file_exists ( "./utils.php" )) { ? ? ? ? ? ?throw new RuntimeException ( "markutils.php文件丟失,無法進行配置文件的初始化操作!/markbr /" );
}else {
require './utils.php';
} ? ? ? ?// 初始化 配置文件信息
$this-dbconfig = XMLUtil::getDBConfiguration (); ? ? ? ?// 準備好數(shù)據(jù)庫連接池“偽隊列”
$this-poolsize = $poolsize;
$this-dbpool = array (); ? ? ? ?for($index = 1; $index = $this-poolsize; $index ++) {
$conn = mysqli_connect ( $this-dbconfig ['host'], $this-dbconfig ['user'], $this-dbconfig ['password'], $this-dbconfig ['db'] ) or die ( "mark連接數(shù)據(jù)庫失敗!/markbr /" );
array_push ( $this-dbpool, $conn );
}
} ? ?/**
* 從數(shù)據(jù)庫連接池中獲取一個數(shù)據(jù)庫鏈接資源
*
* @throws ErrorException
* @return mixed
*/
public function getConn() { ? ? ? ?if (count ( $this-dbpool ) = 0) { ? ? ? ? ? ?throw new ErrorException ( "mark數(shù)據(jù)庫連接池中已無鏈接資源,請稍后重試!/mark" );
} else { ? ? ? ? ? ?return array_pop ( $this-dbpool );
}
} ? ?/**
* 將用完的數(shù)據(jù)庫鏈接資源放回到數(shù)據(jù)庫連接池
*
* @param unknown $conn
* @throws ErrorException
*/
public function release($conn) { ? ? ? ?if (count ( $this-dbpool ) = $this-poolsize) { ? ? ? ? ? ?throw new ErrorException ( "mark數(shù)據(jù)庫連接池已滿/markbr /" );
} else {
array_push ( $this-dbpool, $conn );
}
}
}
php連接mysql數(shù)據(jù)庫腳本
?php
$con=mysql_connect("localhost","root","root");//分別填寫主機(mysql數(shù)據(jù)庫所在服務器)、用戶名(數(shù)據(jù)庫登錄賬戶名)、密碼
if(!$con)//如果沒有成功連接數(shù)據(jù)庫則執(zhí)行以下語句
{
die("無法連接到數(shù)據(jù)庫:".mysql_error());//die函數(shù)為退出腳本,之后的語句將不被執(zhí)行,直接跳出,括號中是字符串或整數(shù),如果是字符串,則會在終止腳本前輸出字符串,如果是0~254的整數(shù),將不會輸出。mysql_error()函數(shù)可以顯示mysql出錯信息,便于調(diào)試
}
mysql_select_db("games",$con);//在這里直接進入數(shù)據(jù)庫games,方便以后連接,如果該賬戶有多個數(shù)據(jù)庫,此句省略也可,在需要連接數(shù)據(jù)庫時再進行選擇
?
網(wǎng)站標題:php連接數(shù)據(jù)庫小項目,PHP如何連接數(shù)據(jù)庫
新聞來源:http://chinadenli.net/article18/hesedp.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設、網(wǎng)站策劃、動態(tài)網(wǎng)站、網(wǎng)站設計公司、移動網(wǎng)站建設、
聲明:本網(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)