oracle 用php连接oracle数据库并执行sql?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14335668/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 01:23:02  来源:igfitidea点击:

connect oracle database with php and execute sql?

phpsqloracle

提问by Alesfatalis

Hey guys im trying to connect to an oracle database with php. I tried it like i do it with mysql. How to do it like this:

嘿伙计们,我正在尝试使用 php 连接到 oracle 数据库。我像用 mysql 一样尝试过。如何做到这一点:

$host="localhost";
$user="username";
$pass="password";
$db="database";

$link = mysql_connect($host, $user, $pass) or die ("Keine Verbindung zu der Datenbank moeglich.");

mysql_select_db($db, $link);

$sql = "SQL query goes here";

$result = mysql_query($sql);

How can i do exact this with an oracle database. I have the following connection details sid, ip, port, username, password.

我怎样才能用 oracle 数据库做到这一点。我有以下连接详细信息 sid, ip, port, username, password

回答by Alen Oblak

Simple script:

简单的脚本:

$DB = '//1.2.3.4:1521/XE';
$DB_USER = 'user';
$DB_PASS = 'pass';
$DB_CHAR = 'AL32UTF8';

$conn = oci_connect($DB_USER, $DB_PASS, $DB, $DB_CHAR);
$statement = oci_parse($conn, 'select 1 from dual');
oci_execute($statement);
$row = oci_fetch_array($statement, OCI_ASSOC+OCI_RETURN_NULLS);

回答by kokx

To connect with an Oracle database, you don't use the mysql extension (since that is for MySQL). You should use PDO, with the OCI/Oracle adapter.

要连接 Oracle 数据库,不要使用 mysql 扩展(因为它是用于 MySQL)。您应该使用 PDO 和OCI/Oracle 适配器

回答by RMcLeod

You'll want to use PDO to connect to Oracle here is the PHP manual pageon creating a connection using PDO, the example given is for MySQL but it will work fine with Oracle. You will need to ensure the PDO:Oracle extension is installed and running on your PHP configuration.

您将要使用 PDO 连接到 Oracle,此处是有关使用 PDO 创建连接的PHP 手册页,给出的示例适用于 MySQL,但它适用于 Oracle。您需要确保 PDO:Oracle 扩展已安装并在您的 PHP 配置上运行。