Heim > php教程 > php手册 > 写了一个设置pdo DSN的方法想请大家看看

写了一个设置pdo DSN的方法想请大家看看

WBOY
Freigeben: 2016-06-13 09:52:36
Original
1093 Leute haben es durchsucht




写了一个设置pdo DSN的方法想请大家看看

ORACLE跟SQLITE 都只有数据库主机,连数据库名都没有,我不知道怎么设置。
发出来大家看看吧 private function setDSN()

{

switch (strtoupper($this->datatype)){

case 'MYSQL':

$_DSN = 'mysql:host='.$this->hostname.';dbname='.$this->database.';port='.$this->hostport;

break;

case 'MSSQL':

case 'DB2':

$_DSN = 'dblib:host='.$this->hostname.':'.$this->hostport.';dbname='.$this->database;

break;

case 'ORACLE':

$_tns = "

(DESCRIPTION =

(ADDRESS_LIST =

(ADDRESS = (PROTOCOL = TCP )(HOST = $this->hostname)(PORT = $this->hostport))

)

(CONNECT_DATA =

(SERVICE_NAME = $this->database)

)

)";

$_DSN = 'oci:dbname='.$_tns;

break;

case 'SQLITE':

$_DSN = 'sqlite:'.$this->hostname;

break;

case 'PGSQL':

$_DSN = 'pgsql:host='.$this->hostname.'port='.$this->hostport.';dbname='.$this->database;

break;

case 'FIREBIRD':

$_DSN = 'firebird:dbname='.$this->hostname.':'.$this->database;

break;

case 'ODBC':

$_DSN = 'odbc:DSN='.$this->hostname.';UID='.$this->username.';PWD='.$this->password;

break;

}

return $_DSN;

}


复制代码ORACLE 看了一下cn.php.net上的又修改了一下。

[ ]

我来回答




D8888D回贴内容-------------------------------------------------------
我是来学习的[img]http://www.111cn.cn/bbs/images/smilies/default/lol.gif[/img]

D8888D回贴内容-------------------------------------------------------
分少的原因?怎么没人回复呢`

D8888D回贴内容-------------------------------------------------------
见过

D8888D回贴内容-------------------------------------------------------
这两个数据库 都木用过

D8888D回贴内容-------------------------------------------------------
学习一下,只看程序思路,不看用啥数据库

D8888D回贴内容-------------------------------------------------------
请查阅手册上相关的资料
Example#1 PDO_SQLITE DSN examples

The following examples show PDO_SQLITE DSN for connecting to SQLite databases: sqlite:/opt/databases/mydb.sq3

sqlite::memory:

sqlite2:/opt/databases/mydb.sq2

sqlite2::memory:
复制代码
cursade at hotmail dot com
2006-04-21 14:29
if oracle and oracle instant client has been installed,
without db in the same host

For UNIX/LINUX,set $LD_LIBRARY_PATH
appent your instant client path and client/lib path to it,

For windows set PATH like this

After set the path ,set TNS_ADMIN everioment ,point to
where tnsnames.ora located.

Then,you can use service name to connect to your Database

Test coding

$param = $_POST;

$db_username = "youusername";

$db_password = "yourpassword";

$db = "oci:dbname=yoursid";

$conn = new PDO($db,$db_username,$db_password);

$name = $param['module'];

$file = $param['file'];

$stmt = $conn->exec("INSERT INTO AL_MODULE (AL_MODULENAME, AL_MODULEFILE) VALUES ('$name', '$file')");



?>

复制代码cursade at hotmail dot com
2006-04-20 17:43
If instant client has been installed but the full oracle client
not yet ,you can use pdo to connect to oracle database
like following coding:

$tns = "

(DESCRIPTION =

(ADDRESS_LIST =

(ADDRESS = (PROTOCOL = TCP)(HOST = yourip)(PORT = 1521))

)

(CONNECT_DATA =

(SERVICE_NAME = orcl)

)

)

";

$db_username = "youname";

$db_password = "yourpassword";

try{

$conn = new PDO("oci:dbname=".$tns,$db_username,$db_password);

}catch(PDOException $e){

echo ($e->getMessage());

}

?>

复制代码
1、SQLite不是数据主机,而是数据库文件
2、ORACEL的主机格式可能有多种形式,说不定人家是一个数据集群呢?

[ ]


Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Empfehlungen
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage