百科狗-知识改变命运!
--

ODBC and DB2(PDO) - PDO驱动

是丫丫呀12个月前 (11-21)阅读数 18#技术干货
文章标签这是

ODBC and DB2(PDO)

简介

PDO_ODBC is a driver that implements the PHP Data Objects(PDO)interface to enable access from PHP to databases through ODBC drivers or through the IBM DB2 Call Level Interface(DB2 CLI)library. PDO_ODBC currently supports three different "flavours" of database drivers:ibm-db2

Supports access to IBM DB2 Universal Database, Cloudscape, and Apache Derby servers through the free DB2 express-C client.

unixODBC

Supports access to database servers through the unixODBC driver manager and the database's own ODBC drivers.

generic

Offers a compile option for ODBC driver managers that are not explicitly supported by PDO_ODBC.

ODBC and DB2(PDO) - PDO驱动

On Windows,php_pdo_odbc.dllhas to be enabled as extension inphp.ini. It is linked against the Windows ODBC Driver Manager so that PHP can connect to any database cataloged as a System DSN, and is the recommended driver for connecting to Microsoft SQL Server databases.

安装

PDO_ODBC on UNIX systems
  1. As of PHP 5.1, PDO_ODBC is included in the PHP source. You can compile the PDO_ODBC extension as either a static or shared module using the following configure commands.ibm_db2

    ./configure --with-pdo-odbc=ibm-db2,/opt/IBM/db2/V8.1/
    
    To build PDO_ODBC with the ibm-db2 flavour, you have to have previously installed the DB2 application development headers on the same machine on which you are compiling PDO_ODBC. The DB2 application development headers are an installable option in the DB2 servers, and are also available as part of the DB2 Application Development Client freely available for download from the IBM developerWorks » website.

    If you do not supply a location for the DB2 libraries and headers to the configure command, PDO_ODBC defaults to/home/db2inst1/sqllib.

    unixODBC

    ./configure --with-pdo-odbc=unixODBC,/usr/local
    
    If you do not supply a location for the unixODBC libraries and headers to the configure command, PDO_ODBC defaults to/usr/local.generic
    ./configure --with-pdo-odbc=generic,/usr/local,libname,ldflags,cflags
    

运行时配置

这些函数的行为受php.ini中的设置影响。

PDO_ODBC Configuration Options
名字默认可修改范围更新日志
pdo_odbc.connection_pooling"strict"PHP_INI_ALLAvailable since PHP 5.1.0.
pdo_odbc.db2_instance_nameNULLPHP_INI_SYSTEMAvailable since PHP 5.1.1.本过时特性肯定会在未来被移除
有关 PHP_INI_*样式的更多详情与定义,见配置可被设定范围。

这是配置指令的简短说明。

pdo_odbc.connection_poolingstring

Whether to pool ODBC connections. Can be one of"strict","relaxed"or"off"(equals to""). The parameter describes how strict the connection manager should be when matching connection parameters to existing pooled connections.strict is the recommend default, and will result in the use of cached connections only when all the connection parameters match exactly.relaxed will result in the use of cached connections when similar connection parameters are used. This can result in increased use of the cache, at the risk of bleeding connection information between(for example)virtual hosts.

This setting can only be changed from thephp.inifile, and affects the entire process; any other modules loaded into the process that use the same ODBC libraries will be affected too, including the Unified ODBC extension.

Warning

relaxed matching should not be used on a shared server, for security reasons.

Tip

Leave this setting at the default strict setting unless you have good reason to change it.

pdo_odbc.db2_instance_namestring

If you compile PDO_ODBC using thedb2flavour, this setting sets the value of the DB2INSTANCE environment variable on Linux and UNIX operating systems to the specified name of the DB2 instance. This enables PDO_ODBC to resolve the location of the DB2 libraries and make cataloged connections to DB2 databases.

This setting can only be changed from thephp.inifile, and affects the entire process; any other modules loaded into the process that use the same ODBC libraries will be affected too, including the Unified ODBC extension.

This setting has no effect on Windows.

Table of Contents

  • PDO_ODBC DSN— Connecting to ODBC or DB2 databases
I just spent a couple of hours trying to track down the Exception "Could not find driver". This was despite having ODBC and PDO_ODBC installed, and all of the configuration seemed to be correct.
Turned out the problem was that I used ODBC in upper-case in the dsn. As soon as I changed the dns to "odbc:database" it worked.
As this code used to work a few months ago, this sudden case-sensitivity threw me for a loop. So in case you get this error, check the casing first.
If you want to avoid installing DB2 Connect and/or PECL modules ibm_db2 and PDO_IBM, you can also use IBM DB2 databases trough unixODBC.
If you have DB2 database on a i server you need to install IBM iAccess (http://www.ibm.com/systems/i/software/access/linux/index.html) and unixODBC. Just install the libraries (rpm) and modify configurations in /etc/odbcinst.ini (sample configuration in /opt/ibm/iSeriesAccess/unixodbcregistration) and /etc/odbc.ini.
To my experience this is much easier way than installing DB2 Connect.
Using SQL Server Native Client 11.0 on Linux as a PDO_ODBC driver:
Download the SQL Server Native Client 11.0 on Linux ODBC Driver:
http://www.microsoft.com/download/en/details.aspx?id=28160
Configuration ODBC:
/usr/local/etc/odbcsys.ini
--
[SQL Server Native Client 11.0]
Description = Microsoft SQL Server ODBC Driver V1.0 for Linux
Driver = /opt/microsoft/sqlncli/lib64/libsqlncli-11.0.so.1720.0
UsageCount = 1
/usr/local/etc/odbc.ini
--
[MSSQLServer]
Driver = SQL Server Native Client 11.0
Description = Sample Database
Trace = Yes
Server = 
Port = 1433
Database = 
Test the connection:
mssqltest.php
-- 
Using SQL 2005, PDO_ODBC and datetime fields is a royal pain. MSDN documentation on CAST CONVERT shows that there is supposed to be an implicit convert between character types and datetime types. That's true... until you put it in a stored procedure and use variable declarations.
For instance this fails:
declare @date varchar;
SET @date = '20080101';
SELECT cast(@date AS datetime) AS poo
While this succeeds:
declare @date varchar(19);
SET @date = '20080101';
SELECT cast(@date AS datetime) AS poo
The PDO Driver appears to attempt an implicit conversion and so it fails whenever you try to insert data into datetime column types.
So to workaround this nuance in SQL, declare a character column type with explicit width. Then your implicit type conversion will work.
MSSQL - PHP on Apache - Linux Redhat
When using php 5.2.10 please beaware of this error:
http://bugs.php.net/bug.php?id=42068
Standard odbc_connect will not work, you must use pdo_odbc
Connecting to MSSQL using pdo odbc - walkthrough.. 
1. Download and configure FreeTDS with-unixodbc
./configure --prefix=/opt/SYSfreetds --with-unixodbc
make;make test; make install
2. install php-odbc and unixODBC
     php-odbc-5.2.10-1.x86_64.rpm
     unixODBC.x86_64.x86x64
3. Setup ODBC links
a)
Create a tds.driver file with the following contents
 [FreeTDS]
 Description   = v0.63 with protocol v8.0
 Driver     = /opt/SYSfreetds/lib/libtdsodbc.so
Register the ODBC driver - the tds.driver file
 odbcinst -i -d -f tds.driver
b)
Creating a tds.datasource file - ODBC Data Source with contents:
 [SOURCENAME]
 Driver=FreeTDS
 Description=Test MS SQL Database with FreeTDS
 Trace=No
 Server=BobTheServer
 Port=1433
 TDS Version=8.0
 Database=youDBname
Register the ODBC data source
 odbcinst -i -s -f tds.datasource
Beware that the odbc.ini file will be installed in the current users home directory. This may need to be used if you are using a webserver as the apache home directory could be different.
Ensure .odbc.ini is in apaches home directory, possibly "/var/www"
4. Test the ODBC link on the command line
 isql -v SOURCENAME 'username' 'password'
 +---------------------------------------+
 | Connected!              |
 |                    |
 | sql-statement             |
 | help [tablename]           |
 | quit                 |
 |                    |
 +---------------------------------------+
 SQL>
5. Edit /etc/php.ini
 Make sure the following is set:
   mssql.secure_connection = On
 
6. Restart apache gracefully
7. PHP to run:
 
Trouble-shooting:
Please try strace/ truss if you encounter issues. It could be you are referencing wrong libraries somewhere.
Ensure you have restarted apache once the odbc files are in place

鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)