Php oracle多数据库查询

时间:2011-12-01 04:53:18

标签: php oracle

Normaly如果mysql或mssql我会在下面做 数据库A:test1 数据库B:test2 数据库C:test3

Sql声明:

select * FROM test1.table1  
join test2  on test1.table1.id  = test2.table2.id
join test3  on test1.table1.id  = test3.table4.id 

它如何在Oracle和Php中运行?

2 个答案:

答案 0 :(得分:0)

您是否在使用MySQL的PHP​​中尝试完全相同的方式?

<?php

$sql_statement = "SELECT
    PostID,
    AuthorID,
    Users.tblUsers.Username
    FROM tblPosts
    LEFT JOIN Users.tblUsers ON AuthorID = Users.tblUsers.UserID
    GROUP BY PostID,AuthorID,Username
    ";

$dblink = mysql_connect("somehost", "someuser", "password");
mysql_select_db("BlogPosts",$dblink);
$qry = mysql_query($sql_statement,$dblink);

?>

答案 1 :(得分:0)

好的,您首先需要在oracle中创建一个数据库链接:

CREATE DATABASE LINK dblink_name 
   CONNECT TO username IDENTIFIED BY password
   USING 'service_name';

Service_name是数据库的tnsnames.ora中的记录。

或者,如果您不想使用tnsnames.ora,您可以在create function中指定您的网名,如下所示:

CREATE DATABASE LINK dblink_name 
     CONNECT TO username IDENTIFIED BY password
     USING '(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=hostname)(PORT=1521))(CONNECT_DATA(SID=dbname))';

然后以这种方式使用它:

SELECT host_name, instance_nmame FROM v$instance@dblink_name;