假设你已经成功安装了Oracle11g,有一个本地的Oracle服务器,或者你有一个远程的Oracle服务器。注意保证客户端和服务器端体系结构的统一性(笔者统一为32位)。
右键win32_11gR2_client.zip
,解压到当前文件夹。
双击client
文件夹下的setup.exe
执行程序。
先决条件检查出现报错,按Oracle11g安装教程中的解决办法增加Windows10选项即可。
选择安装类型:选择管理员,单击下一步。
选择产品语言:默认的简体中文和英语即可,单击下一步。
指定安装位置:更改盘符为你的软件盘即可,单击下一步。
体系结构报错勾选全部忽略,单击下一步,然后单击完成。
安装完成后单击关闭选项。
Net Configuration Assistant
,单击运行。如果你严格安装笔者的安装顺序和相关配置,那么在你打开Toad后,会自动有一个服务名位ORCL,输入Scott及相关密码,然后勾选ORCL,点击connect即可。在Databases-Schema Browser下即可看到Scott用户下的数据库表。
打开php.ini,输入oci,去掉;extension=php_pdo_oci.dll
前面的分号,重启服务器。
在网站根目录下写文件:phpinfo.php
phpinfo();
访问http://localhost:8080/phpinfo.php
,输入pdo搜索可以看见如下配置信息,可知开启了oci扩展。
PDO support | enabled |
---|---|
PDO drivers | sqlsrv, mysql, oci, odbc, sqlite |
4. 在网站根目录下编写Oracle.php
,编写完成后即可访问到数据。
“`php
thinkPHP框架连接Oracle需要安装think-oracle库,使用composer
安装比较方便:composer.json
{
"name": "topthink/think",
"description": "the new thinkphp framework",
"type": "project",
"keywords": [
"framework",
"thinkphp",
"ORM"
],
"homepage": "http://thinkphp.cn/",
"license": "Apache-2.0",
"authors": [
{
"name": "liu21st",
"email": "[email protected]"
}
],
"require": {
"php": ">=5.4.0",
"topthink/framework": "5.0.*",
"topthink/think-image": "^1.0",
"topthink/think-captcha": "1.*",
"topthink/think-mongo": "1.*",
"topthink/think-migration": "1.*",
"topthink/think-queue": "1.*",
"topthink/think-oracle": "1.*",
"topthink/think-helper": "1.*"
},
"autoload": {
"psr-4": {
"app\\": "application"
}
},
"extra": {
"think-path": "thinkphp"
},
"config": {
"preferred-install": "dist"
}
}
如果不采用composer发布安装,去官网下载thinkPHP完整版解压命名为oracle文件夹并移到网站根目录,去github上搜索下载think-oracle,下载解压后将think-oracle-master重命名think-oracle,并将think-oracle文件夹复制到vendor\top-think\
文件夹下并执行第三步和第四部的操作,使用composer请直接跳转到第5步。
修改\vendor\composer\autoload_psr4.php
:
// autoload_psr4.php @generated by Composer
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
'think\\oracle\\' => array($vendorDir . '/topthink/think-oracle/src'), // 添加此行
'think\\mongo\\' => array($vendorDir . '/topthink/think-mongo/src'),
'think\\migration\\' => array($vendorDir . '/topthink/think-migration/src'),
'think\\helper\\' => array($vendorDir . '/topthink/think-helper/src'),
'think\\composer\\' => array($vendorDir . '/topthink/think-installer/src'),
'think\\captcha\\' => array($vendorDir . '/topthink/think-captcha/src'),
'think\\' => array($baseDir . '/thinkphp/library/think', $vendorDir . '/topthink/think-image/src', $vendorDir . '/topthink/think-queue/src'),
'app\\' => array($baseDir . '/application'),
'Phinx\\' => array($vendorDir . '/topthink/think-migration/phinx/src/Phinx'),
);
修改\vendor\composer\autoload_psr4.php
:
// autoload_static.php @generated by Composer
namespace Composer\Autoload;
class ComposerStaticInit011b7d796a0e5197502be84884b17596
{
public static $files = array (
'9b552a3cc426e3287cc811caefa3cf53' => __DIR__ . '/..' . '/topthink/think-helper/src/helper.php',
'1cfd2761b63b0a29ed23657ea394cb2d' => __DIR__ . '/..' . '/topthink/think-captcha/src/helper.php',
'ddc3cd2a04224f9638c5d0de6a69c7e3' => __DIR__ . '/..' . '/topthink/think-migration/src/config.php',
'cc56288302d9df745d97c934d6a6e5f0' => __DIR__ . '/..' . '/topthink/think-queue/src/common.php',
);
public static $prefixLengthsPsr4 = array (
't' =>
array (
'think\\oracle\\' => 13,
'think\\mongo\\' => 12,
'think\\migration\\' => 16,
'think\\helper\\' => 13,
'think\\composer\\' => 15,
'think\\captcha\\' => 14,
'think\\' => 6,
),
'a' =>
array (
'app\\' => 4,
),
'P' =>
array (
'Phinx\\' => 6,
),
);
public static $prefixDirsPsr4 = array (
'think\\oracle\\' =>
array (
0 => __DIR__ . '/..' . '/topthink/think-oracle/src',
), // 添加此行
'think\\mongo\\' =>
array (
0 => __DIR__ . '/..' . '/topthink/think-mongo/src',
),
'think\\migration\\' =>
array (
0 => __DIR__ . '/..' . '/topthink/think-migration/src',
),
'think\\helper\\' =>
array (
0 => __DIR__ . '/..' . '/topthink/think-helper/src',
),
'think\\composer\\' =>
array (
0 => __DIR__ . '/..' . '/topthink/think-installer/src',
),
'think\\captcha\\' =>
array (
0 => __DIR__ . '/..' . '/topthink/think-captcha/src',
),
'think\\' =>
array (
0 => __DIR__ . '/../..' . '/thinkphp/library/think',
1 => __DIR__ . '/..' . '/topthink/think-image/src',
2 => __DIR__ . '/..' . '/topthink/think-queue/src',
),
'app\\' =>
array (
0 => __DIR__ . '/../..' . '/application',
),
'Phinx\\' =>
array (
0 => __DIR__ . '/..' . '/topthink/think-migration/phinx/src/Phinx',
),
);
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInit011b7d796a0e5197502be84884b17596::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit011b7d796a0e5197502be84884b17596::$prefixDirsPsr4;
}, null, ClassLoader::class);
}
}
修改application\database.php
return [
// 数据库类型
'type' => '\think\oracle\Connection',
// 服务器地址
'hostname' => '127.0.0.1',
// 数据库名
'database' => 'orcl',
// 用户名
'username' => 'Scott',
// 密码
'password' => '', // 此处为你的Scott密码
// 端口
'hostport' => '1521',
// 连接dsn
'dsn' => '',
// 数据库连接参数
'params' => [],
// 数据库编码默认采用utf8
'charset' => 'utf8',
// 数据库表前缀
'prefix' => '',
// 数据库调试模式
'debug' => true,
// 数据库部署方式:0 集中式(单一服务器),1 分布式(主从服务器)
'deploy' => 0,
// 数据库读写是否分离 主从式有效
'rw_separate' => false,
// 读写分离后 主服务器数量
'master_num' => 1,
// 指定从服务器序号
'slave_no' => '',
// 是否严格检查字段是否存在
'fields_strict' => true,
// 数据集返回类型
'resultset_type' => 'array',
// 自动写入时间戳字段
'auto_timestamp' => false,
// 时间字段取出后的默认时间格式
'datetime_format' => 'Y-m-d H:i:s',
// 是否需要进行SQL性能分析
'sql_explain' => false,
];
修改application\index\controller\Index.php
:
namespace app\index\controller;
use think\Db;
use think\Controller;
class Index extends Controller
{
public function index()
{
$oracle_data = Db::name('emp')->select();
print_r($oracle_data);
}
}
访问http://localhost:8080/oracle/public/
即可看到已经取出数据。
关于thinkPHP数据库操作方法改写请见thinkPHP中MySQL迁移到Oracle一文。