GBase 8s数据库连接 - PHP ODBC
Linux下通过PHP(ODBC)连接到GBase 8s数据库
操作系统:RHEL 6U9 64bit
必需组件:
unixODBC unix下的odbc
php 操作系统自带的php
php-odbc odbc连接php组件
httpd apache httpd服务器
数据库连接工具:
GBase 8s CSDK
配置前提
1,GBase 8s 数据库服务器已经正常启动
2,GBase 8s 客户端连接工具已经安装
1,系统环境确认
1.1 php、php-odbc、httpd及unixODBC版本
Linux下root用户通过rpm确认php、php-odbc、unixODBC、httpd均已经安装,如下:
[root@rhel6u9 ~]# rpm -qa php php-odbc unixODBC httpd
httpd-2.2.15-59.el6.x86_64
unixODBC-2.2.14-14.el6.x86_64
php-odbc-5.3.3-49.el6.x86_64
php-5.3.3-49.el6.x86_64
1.2 GBase 8s CSDK已经安装
Linux下gbasedbt用户通过esql -V命令确认GBase 8s CSDK已经安装,如下:
[gbasedbt@rhel6u9 ~]$ esql -V
GBase CSDK Version 4.10, GBASE-ESQL Version 4.10.FC4G1_2.0.1A2_2
Software Serial Number AAA#B000000
确认CSDK安装目录下的数据库sqlhosts配置文件正确配置。
2,ODBC配置
2.1 ODBC配置文件
修改/etc/odbc.ini配置文件,增加GBase 8s数据库连接信息
;---------------------------------------------------------------------------
; GBase ODBC Sample File
;
; File: odbc.ini
;
;---------------------------------------------------------------------------
[ODBC Data Sources]
gbase01=GBase ODBC DRIVER
;
; Define ODBC Database Driver's Below - Driver Configuration Section
;
[gbase01]
Driver=/opt/gbasedbt/lib/cli/iclis09b.so
Description=GBase ODBC DRIVER
Database=testdb
LogonID=gbasedbt
pwd=GBase123
Servername=gbase01
CursorBehavior=0
CLIENT_LOCALE=zh_CN.utf8
DB_LOCALE=zh_CN.utf8
TRANSLATIONDLL=/opt/gbasedbt/lib/esql/igo4a304.so
;
; UNICODE connection Section
;
[ODBC]
;uncomment the below line for UNICODE connection
UNICODE=UCS-4
;
; Trace file Section
;
Trace=0
TraceFile=/tmp/odbctrace.out
InstallDir=/opt/gbasedbt
TRACEDLL=idmrs09a.so
2.2 验证ODBC配置
加载GBase 8s CSDK环境变量,包含连接到GBase 8s数据库所需的GBASEDBTDIR、LD_LIBRARY_PATH,以及选用的字符集DB_LOCALE、CLIENT_LOCALE、LANG等。加载ODBCINI环境变量。
export GBASEDBTDIR=/opt/gbasedbt
export ODBCINI=/etc/odbc.ini
export LD_LIBRARY_PATH=$GBASEDBTDIR/lib:$GBASEDBTDIR/lib/cli:$GBASEDBTDIR/lib/esql:$LD_LIBRARY_PATH
通过isql命令检查ODBC配置
[root@rhel6u9 ~]# isql -v gbase01
+---------------------------------------+
| Connected! |
| |
| sql-statement |
| help [tablename] |
| quit |
| |
+---------------------------------------+
SQL> select dbservername from dual;
+---------------------------------------+
| |
+---------------------------------------+
| gbase01 |
+---------------------------------------+
SQLRowCount returns -1
1 rows fetched
3,httpd配置
3.1 将环境变量增加到/etc/sysconfig/httpd
GBASEDBTDIR=/opt/gbasedbt
ODBCINI=/etc/odbc.ini
LD_LIBRARY_PAT=$GBASEDBTDIR/lib:$GBASEDBTDIR/lib/cli:$GBASEDBTDIR/lib/esql:$LD_LIBRARY_PATH
通过apachectl restart重启apache http服务器,使配置生效
4, 测试通过php odbc连接到GBase 8s数据库
4.1 在/var/www/html/下创建测试连接的odbc.php,内容如下
<?php
$conn=odbc_connect('gbase01','',''); # 连接到 dsn = gbasedb
if (!$conn)
{exit("Connection Failed: " . $conn);}
$sql="SELECT tabid,tabname FROM systables limit 10";
$rs=odbc_exec($conn,$sql);
if (!$rs)
{exit("Error in SQL");}
echo "<table><tr>";
echo "<th>ID</th>";
echo "<th>表名</th></tr>";
while (odbc_fetch_row($rs))
{
$col1=odbc_result($rs,"tabid");
$col2=odbc_result($rs,"tabname");
echo "<tr><td>$col1</td>";
echo "<td>$col2</td></tr>";
}
odbc_close($conn);
echo "</table>";
4.2 通过php -e odbc.php测试连接
[root@rhel6u9 html]# php -e odbc.php
<table><tr><th>ID</th><th>表名</th></tr><tr><td>1</td><td>systables</td></tr><tr><td>2</td><td>syscolumns</td></tr><tr><td>3</td><td>sysindices</td></tr><tr><td>4</td><td>systabauth</td></tr><tr><td>5</td><td>syscolauth</td></tr><tr><td>6</td><td>sysviews</td></tr><tr><td>7</td><td>sysusers</td></tr><tr><td>8</td><td>sysdepend</td></tr><tr><td>9</td><td>syssynonyms</td></tr><tr><td>10</td><td>syssyntable</td></tr></table>
4.3 通过网页浏览器测试连接
完成配置。