php PDO ODBC DB2 结构
php PDO ODBC DB2 construct
我在构建 PDO 时遇到问题。这是我以前从未使用过的功能。假设这些是我的连接变量
$database = "BLUDB";
$hostname = "dashdb-enterprise-FAKE.services.dal.bluemix.net";
$user = "fakeuser";
$password = "fakepsw";
$port = 50000;
$driver = "DRIVER={IBM DB2 ODBC DRIVER};";
$dsn = "DATABASE=$database; " .
"HOSTNAME=$hostname;" .
"PORT=$port; " .
"PROTOCOL=TCPIP; " .
"UID=$user;" .
"PWD=$password;";
$conn_string = $driver . $dsn;
我试过这个:
$conn = new PDO( "DB2:DRIVER={IBM DB2 ODBC DRIVER},HOSTNAME=dashdb-enterprise-FAKE.services.dal.bluemix.net,PORT=50000,DATABASE=BLUDB,PROTOCOL=TCPIP,UID=fakeuser,PWD=fakepsw");
和其他一些排列。但我不断收到此错误:
Fatal error: Uncaught PDOException: could not find driver in /home/robur12/domains/idobczyce.pl/public_html/mike/dava/index.php:41 Stack trace: #0 /home/robur12/domains/idobczyce.pl/public_html/mike/dava/index.php(41): PDO->__construct('DB2:DRIVER={IBM...') #1 {main} thrown in /home/robur12/domains/idobczyce.pl/public_html/mike/dava/index.php on line 41
我已按照此处描述的过程进行操作:https://www.ibm.com/support/knowledgecenter/en/SSHRBY/com.ibm.swg.im.dashdb.doc/connecting/connect_connecting_php.html
我已经完成安装,我什至 运行 在 shell
中有验证命令
db2cli 验证 -dsn DashDB -connect -user bluadmin -passwd 'your_secret_password'
查看下面的输出
我运行正在Linux s90.linuxpl.com 4.4.27-grsec #1 SMP Tue Oct 25 20:38:40 CEST 2016 x86_64
我的php.ini只说
extension=odbc.so
在网络浏览器中 phpinfo();
将 return
'./configure' '--prefix=/usr/local/php7.3' '--disable-debug' '--disable-ipv6' '--enable-bcmath' '--enable-calendar' '--enable-ctype' '--enable-exif' '--enable-ftp' '--enable-gd-native-ttf' '--enable-inline-optimization' '--enable-mbstring' '--enable-pcntl' '--enable-pdo' '--enable-session' '--enable-soap' '--enable-sockets' '--enable-sysvsem' '--enable-wddx' '--enable-zip' '--with-bz2' '--with-config-file-path=/usr/local/php7.3' '--with-pear=/usr/local/php7.3/lib/php' '--with-curl=/usr/local/lib' '--with-freetype-dir' '--with-freetype-dir=/usr/local/lib' '--with-gd' '--with-gdbm' '--with-gettext' '--with-iconv=/usr/local' '--with-imap-ssl' '--with-imap=/usr/lib/' '--with-jpeg-dir=/usr/local/lib' '--with-kerberos' '--with-ldap' '--with-ldap-sasl' '--with-mcrypt' '--with-mhash' '--with-mysqli=mysqlnd' '--with-openssl=/usr/local/adds/openssl' '--with-pcre-regex' '--with-pdo-mysql=mysqlnd' '--with-png-dir=/usr/local/lib' '--with-pspell' '--with-readline' '--with-tidy' '--with-xmlrpc' '--with-xsl' '--with-zlib' '--with-zlib-dir=/usr/local/lib' '--enable-opcache' '--with-pdo-pgsql=/usr/local/pgsql' '--with-pgsql=/usr/local/pgsql' '--enable-intl' '--with-pdo-odbc=ibm-db2,/usr/local/adds/dsdriver'
但是当我在命令行中 运行 php -m
时,我没有看到 ibm_db2 也没有看到 pdo_ibm。我只看到以下内容:
我已经联系了我的 php 服务器提供商,然后询问:
"can you possibly instal the following php modules: pdo_ibm and ibm_db2?"
他们回复:
“您似乎正在尝试访问 IBM DB2。
您应该按照此处的说明使用 PDO 功能:
https://www.php.net/manual/en/ref.pdo-odbc.php
我们已经在星期五为您编译了 pdo ibm_db2。
ODBC 的 PDO 驱动程序 (ibm-db2) => 已启用
如果您需要安装其他模块,请提供 link 或文档
对于某些驱动程序版本,您需要指定前缀 ibm:
才能使用 Db2 的 IBM pdo 扩展。
此外,还有几个先决条件需要先安装和配置。它有助于在您尝试连接之前分别验证每个先决条件。您应该为您的 PHP 版本安装 php-cli 以帮助确定问题(例如 php7.0-cli)。
特别是,在 shell 命令行(在运行 PHP 的主机名上)验证 php -m
显示 pdo_ibm 和 ibm_db2 是加载。如果其中之一或两者都缺失,那么您的 php.ini 未配置,或者驱动程序未加载(在这种情况下,您应该在 php -m
输出的开头看到一条错误消息。
这是一个显示完整连接字符串而不是编目数据库的示例:
<?php
#
# This shows using a connection-string to a Db2-on-cloud database.
#
# Prerequisites:
# 0. Follow IBM's documenation at https://www.ibm.com/support/knowledgecenter/SSEPGG_11.5.0/com.ibm.swg.im.dbclient.php.doc/doc/c0054947.html
# 1. Modules ibm_db and pdo_ibm are already installed to match the PHP
# 2. The php.ini loads the ibm_db and pdo_ibm drivers successfully
# 3. You already verified your Db-client is operating successfully (use db2cli validate and db2cli validate -connect).
#
$database = 'bludb';
$user = 'whatever';
$password = 'whatever';
$hostname = 'dashdb-txn-sbox-whatever.bluemix.net';
$port = 50000;
$string= sprintf("ibm:DRIVER={IBM DB2 ODBC DRIVER};DATABASE=%s;HOSTNAME=%s;PORT=%d;PROTOCOL=TCPIP;",
$database ,
$hostname ,
$port);
try {
$connection = new PDO(
$string,$user,$password, array(PDO::ATTR_PERSISTENT => TRUE, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
$req=$connection->query("select * from syscat.views where viewschema not like 'SYS%' and viewschema not like 'IBM%'");
foreach ($req as $row) {
print_r($row);
}
}
catch (Exception $e) {
echo($e->getMessage());
}
?>
事实证明,在 shell 命令行中绑定 php -m
访问了我的 php 7.2 模块。
我需要输入 /usr/local/php7.3/bin/php -m
才能查看我的 php 7.3 模块。
当我这样做时,我看到了 2 个额外的模块:PDO 和 PDO_ODBC。
我必须使用的 php PDO 函数语法是:
$database = 'BLUDB';
$username = 'fakeusername';
$password = 'fakepassword';
$hostname = 'dashdb-fakehostname.bluemix.net';
$port = 50000;
$string= sprintf("odbc:DRIVER{PDO_ODBC};DATABASE=%s;HOSTNAME=%s;PORT=%d;PROTOCOL=TCPIP;",
$database ,
$hostname ,
$port);
$connection = new PDO($string,$username,$password)
我在构建 PDO 时遇到问题。这是我以前从未使用过的功能。假设这些是我的连接变量
$database = "BLUDB";
$hostname = "dashdb-enterprise-FAKE.services.dal.bluemix.net";
$user = "fakeuser";
$password = "fakepsw";
$port = 50000;
$driver = "DRIVER={IBM DB2 ODBC DRIVER};";
$dsn = "DATABASE=$database; " .
"HOSTNAME=$hostname;" .
"PORT=$port; " .
"PROTOCOL=TCPIP; " .
"UID=$user;" .
"PWD=$password;";
$conn_string = $driver . $dsn;
我试过这个:
$conn = new PDO( "DB2:DRIVER={IBM DB2 ODBC DRIVER},HOSTNAME=dashdb-enterprise-FAKE.services.dal.bluemix.net,PORT=50000,DATABASE=BLUDB,PROTOCOL=TCPIP,UID=fakeuser,PWD=fakepsw");
和其他一些排列。但我不断收到此错误:
Fatal error: Uncaught PDOException: could not find driver in /home/robur12/domains/idobczyce.pl/public_html/mike/dava/index.php:41 Stack trace: #0 /home/robur12/domains/idobczyce.pl/public_html/mike/dava/index.php(41): PDO->__construct('DB2:DRIVER={IBM...') #1 {main} thrown in /home/robur12/domains/idobczyce.pl/public_html/mike/dava/index.php on line 41
我已按照此处描述的过程进行操作:https://www.ibm.com/support/knowledgecenter/en/SSHRBY/com.ibm.swg.im.dashdb.doc/connecting/connect_connecting_php.html
我已经完成安装,我什至 运行 在 shell
中有验证命令db2cli 验证 -dsn DashDB -connect -user bluadmin -passwd 'your_secret_password'
查看下面的输出
我运行正在Linux s90.linuxpl.com 4.4.27-grsec #1 SMP Tue Oct 25 20:38:40 CEST 2016 x86_64
我的php.ini只说
extension=odbc.so
在网络浏览器中 phpinfo();
将 return
'./configure' '--prefix=/usr/local/php7.3' '--disable-debug' '--disable-ipv6' '--enable-bcmath' '--enable-calendar' '--enable-ctype' '--enable-exif' '--enable-ftp' '--enable-gd-native-ttf' '--enable-inline-optimization' '--enable-mbstring' '--enable-pcntl' '--enable-pdo' '--enable-session' '--enable-soap' '--enable-sockets' '--enable-sysvsem' '--enable-wddx' '--enable-zip' '--with-bz2' '--with-config-file-path=/usr/local/php7.3' '--with-pear=/usr/local/php7.3/lib/php' '--with-curl=/usr/local/lib' '--with-freetype-dir' '--with-freetype-dir=/usr/local/lib' '--with-gd' '--with-gdbm' '--with-gettext' '--with-iconv=/usr/local' '--with-imap-ssl' '--with-imap=/usr/lib/' '--with-jpeg-dir=/usr/local/lib' '--with-kerberos' '--with-ldap' '--with-ldap-sasl' '--with-mcrypt' '--with-mhash' '--with-mysqli=mysqlnd' '--with-openssl=/usr/local/adds/openssl' '--with-pcre-regex' '--with-pdo-mysql=mysqlnd' '--with-png-dir=/usr/local/lib' '--with-pspell' '--with-readline' '--with-tidy' '--with-xmlrpc' '--with-xsl' '--with-zlib' '--with-zlib-dir=/usr/local/lib' '--enable-opcache' '--with-pdo-pgsql=/usr/local/pgsql' '--with-pgsql=/usr/local/pgsql' '--enable-intl' '--with-pdo-odbc=ibm-db2,/usr/local/adds/dsdriver'
但是当我在命令行中 运行 php -m
时,我没有看到 ibm_db2 也没有看到 pdo_ibm。我只看到以下内容:
我已经联系了我的 php 服务器提供商,然后询问:
"can you possibly instal the following php modules: pdo_ibm and ibm_db2?"
他们回复:
“您似乎正在尝试访问 IBM DB2。
您应该按照此处的说明使用 PDO 功能:
https://www.php.net/manual/en/ref.pdo-odbc.php
我们已经在星期五为您编译了 pdo ibm_db2。 ODBC 的 PDO 驱动程序 (ibm-db2) => 已启用 如果您需要安装其他模块,请提供 link 或文档
对于某些驱动程序版本,您需要指定前缀 ibm:
才能使用 Db2 的 IBM pdo 扩展。
此外,还有几个先决条件需要先安装和配置。它有助于在您尝试连接之前分别验证每个先决条件。您应该为您的 PHP 版本安装 php-cli 以帮助确定问题(例如 php7.0-cli)。
特别是,在 shell 命令行(在运行 PHP 的主机名上)验证 php -m
显示 pdo_ibm 和 ibm_db2 是加载。如果其中之一或两者都缺失,那么您的 php.ini 未配置,或者驱动程序未加载(在这种情况下,您应该在 php -m
输出的开头看到一条错误消息。
这是一个显示完整连接字符串而不是编目数据库的示例:
<?php
#
# This shows using a connection-string to a Db2-on-cloud database.
#
# Prerequisites:
# 0. Follow IBM's documenation at https://www.ibm.com/support/knowledgecenter/SSEPGG_11.5.0/com.ibm.swg.im.dbclient.php.doc/doc/c0054947.html
# 1. Modules ibm_db and pdo_ibm are already installed to match the PHP
# 2. The php.ini loads the ibm_db and pdo_ibm drivers successfully
# 3. You already verified your Db-client is operating successfully (use db2cli validate and db2cli validate -connect).
#
$database = 'bludb';
$user = 'whatever';
$password = 'whatever';
$hostname = 'dashdb-txn-sbox-whatever.bluemix.net';
$port = 50000;
$string= sprintf("ibm:DRIVER={IBM DB2 ODBC DRIVER};DATABASE=%s;HOSTNAME=%s;PORT=%d;PROTOCOL=TCPIP;",
$database ,
$hostname ,
$port);
try {
$connection = new PDO(
$string,$user,$password, array(PDO::ATTR_PERSISTENT => TRUE, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
$req=$connection->query("select * from syscat.views where viewschema not like 'SYS%' and viewschema not like 'IBM%'");
foreach ($req as $row) {
print_r($row);
}
}
catch (Exception $e) {
echo($e->getMessage());
}
?>
事实证明,在 shell 命令行中绑定 php -m
访问了我的 php 7.2 模块。
我需要输入 /usr/local/php7.3/bin/php -m
才能查看我的 php 7.3 模块。
当我这样做时,我看到了 2 个额外的模块:PDO 和 PDO_ODBC。
我必须使用的 php PDO 函数语法是:
$database = 'BLUDB';
$username = 'fakeusername';
$password = 'fakepassword';
$hostname = 'dashdb-fakehostname.bluemix.net';
$port = 50000;
$string= sprintf("odbc:DRIVER{PDO_ODBC};DATABASE=%s;HOSTNAME=%s;PORT=%d;PROTOCOL=TCPIP;",
$database ,
$hostname ,
$port);
$connection = new PDO($string,$username,$password)