系统Linux CentOS7_x86_64
PHP版本5.6.38
以下参考:http://blog.51cto.com/iceeggplant/1052512
一.oracle客户端安装
下载地址:https://www.oracle.com/technetwork/topics/linuxx86-64soft-092277.html
oracle-instantclient11.1-basic-11.1.0.7.0-1.x86_64.rpm
oracle-instantclient11.1-devel-11.1.0.7.0-1.x86_64.rpm
#rpm -ivh oracle-instantclient11.1-basic-11.1.0.7.0-1.x86_64.rpm
#rpm -ivh oracle-instantclient11.1-devel-11.1.0.7.0-1.x86_64.rpm
配置oracle
1.修改/etc/ld.so.conf 或在ld.so.conf.d文件夹下添加oracle-x86_64.conf文件,写入安装oracle客户端的lib路径:
#echo '/usr/lib/oracle/11.1/client64/lib/' > /etc/ld.so.conf.d/oracle-x86_64.conf
2.64位系统需要创建32位的软链接(这里可能是一个遗留bug,不然后面编译会出问题)
ln -s /usr/lib/oracle/11.1/client64 /usr/lib/oracle/11.2/client
ln -s /usr/include/oracle/11.2/client64 /usr/include/oracle/11.2/client
3.定义环境变量,修改/etc/profile文件,加入如下:
export ORACLE_HOME=/usr/lib/oracle/11.2/client64/
export LD_LIBRARY_PATH=/usr/lib/oracle/11.2/client64:$LD_LIBRARY_PATH
export NLS_LANG="AMERICAN_AMERICA.AL32UTF8"
#source /etc/profile使其生效
二、安装pdo_oci模块
防止pdo_oci对oracle11支持不足(pdo_oci可能不支持oracle11g,需要做个软链接成作为oracle10版本才能编译过去):
# ln -s /usr/include/oracle/11.1 /usr/include/oracle/10.2.0.1
# ln -s /usr/lib/oracle/11.1 /usr/lib/oracle/10.2.0.1
pdo_oci在php安装包里的ext下有,没有的话自行下载
# cd php-5.6.38/ext/pdo_oci
# /usr/local/php/bin/phpize
# ./configure --with-php-config=/usr/local/php/bin/php-config --with-pdo-oci=instantclient,/usr,10.2.0.1
# make && make install
在php.ini配置文件里加 extension=pdo_oci.so
三、安装oci8
oci8在php安装包里的ext下有,没有的话自行下载
# cd php-5.6.38/ext/oci8
# /usr/local/php/bin/phpize
# ./configure --with-php-config=/usr/local/php/bin/php-config --with-oci8=shared,instantclient,/usr/lib/oracle/11.1/client/lib
# make && make install
在php.ini配置文件里加 extension=oci8.so
四、添加Oracle.php
Oracle.php可以下载think-oracle得到,重命名Builder.php和Connection.php为Oracle.php放到hinkphp\library\think\db\的builder和connection文件下,也可以自行创建。
在thinkphp\library\think\db\builder下创建Oracle.php,添加以下代码
1) {
$limitStr = "(numrow>" . $limit[0] . ") AND (numrow<=" . ($limit[0] + $limit[1]) . ")";
} else {
$limitStr = "(numrow>0 AND numrow<=" . $limit[0] . ")";
}
}
return $limitStr ? ' WHERE ' . $limitStr : '';
}
/**
* 设置锁机制
* @access protected
* @param bool|false $lock
* @return string
*/
protected function parseLock($lock = false)
{
if (!$lock) {
return '';
}
return ' FOR UPDATE NOWAIT ';
}
/**
* 字段和表名处理
* @access protected
* @param string $key
* @param array $options
* @param bool $strict
* @return string
*/
protected function parseKey($key, $options = [], $strict = false)
{
$key = trim($key);
if (strpos($key, '$.') && false === strpos($key, '(')) {
// JSON字段支持
list($field, $name) = explode($key, '$.');
$key = $field . '."' . $name . '"';
}
return $key;
}
/**
* 随机排序
* @access protected
* @return string
*/
protected function parseRand()
{
return 'DBMS_RANDOM.value';
}
}
在thinkphp\library\think\db\connector下创建Oracle.php文件,添加以下代码
initConnect(true);
list($tableName) = explode(' ', $tableName);
$sql = "select a.column_name,data_type,DECODE (nullable, 'Y', 0, 1) notnull,data_default, DECODE (A .column_name,b.column_name,1,0) pk from all_tab_columns a,(select column_name from all_constraints c, all_cons_columns col where c.constraint_name = col.constraint_name and c.constraint_type = 'P' and c.table_name = '" . strtoupper($tableName) . "' ) b where table_name = '" . strtoupper($tableName) . "' and a.column_name = b.column_name (+)";
$pdo = $this->linkID->query($sql);
$result = $pdo->fetchAll(PDO::FETCH_ASSOC);
$info = [];
if ($result) {
foreach ($result as $key => $val) {
$val = array_change_key_case($val);
$info[$val['column_name']] = [
'name' => $val['column_name'],
'type' => $val['data_type'],
'notnull' => $val['notnull'],
'default' => $val['data_default'],
'primary' => $val['pk'],
'autoinc' => $val['pk'],
];
}
}
return $this->fieldCase($info);
}
/**
* 取得数据库的表信息(暂时实现取得用户表信息)
* @access public
* @param string $dbName
* @return array
*/
public function getTables($dbName = '')
{
$pdo = $this->linkID->query("select table_name from all_tables");
$result = $pdo->fetchAll(PDO::FETCH_ASSOC);
$info = [];
foreach ($result as $key => $val) {
$info[$key] = current($val);
}
return $info;
}
/**
* 获取最近插入的ID
* @access public
* @param string $sequence 自增序列名
* @return string
*/
public function getLastInsID($sequence = null)
{
if ($sequence === null) {
return '';
}
$pdo = $this->linkID->query("select {$sequence}.currval as id from dual");
$result = $pdo->fetchColumn();
return $result;
}
/**
* SQL性能分析
* @access protected
* @param string $sql
* @return array
*/
protected function getExplain($sql)
{
return [];
}
protected function supportSavepoint()
{
return true;
}
}
五、测试连接
$db = [
// 数据库类型
'type' => 'oracle',
// 服务器地址
'hostname' => '192.168.1.100',
// 数据库名
'database' => 'Test',
// 用户名
'username' => 'Test',
// 密码
'password' => 'Test',
// 端口
'hostport' => '1521',
];
$data = Db::connect($db)->table('Test')->select();
halt($data);