doukangbin9698 2015-01-08 22:23
浏览 56

从PHP连接到Oracle数据库

I have an Oracle database that I am trying to connect to.

For some reason when I try the following code:

<?php
    include "header.php";
    // simply attempt to connect to the database
    /* If you are connecting to the Oracle database, the credentials are as follows: 
     * Username: ********
     * Password: ********
     * Hostname: **********
     * Port: 1521
     * Service name: ***********
    */
    $oracleConnect = true;
    if ($oracleConnect)
    {
        echo 'Attempting connection...<br>';
        $connection = null;
        try
        {
            $connection = oci_connect('user',
                'pass',
                'user@//hostname:1521/dbname');
        }
        catch (Exception $e)
        {
            echo $e->getMessage();
        }
        if (!$connection)
        {
            echo '<p>Something is wrong.</p>';
            $e = oci_error();
            trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
        }
        // if the connection has been established
        else
        {
            // tell the user and close it (this is a test)
            echo 'Connection established!!';
            oci_close($connection);
        }
    }
    else
    {
        $connection = new mysqli('host', 'user', 'password', 'database');
        echo ($connection) ? 'Database connection successful!' : 'Could not connect.';
    }
    include "footer.php";
?>

When I try the above code, I get the "Attempting connection..." to print, but nothing else. It is supposed to print something else regardless. What could possibly be going wrong?

  • 写回答

1条回答 默认 最新

  • douxian9943 2015-01-08 22:24
    关注

    I think the problem is the user@ part of your connection string. I dont think thats necessary as oci_connect has a user name parameter. I could be wrong, ive never used oracle from php before, but the docs on oci connections would also seem to indicate that:

    To use the Easy Connect naming method, PHP must be linked with Oracle 10g or greater Client libraries. The Easy Connect string for Oracle 10g is of the form: [//]host_name[:port][/service_name]. From Oracle 11g, the syntax is: [//]host_name[:port][/service_name][:server_type][/instance_name]. Service names can be found by running the Oracle utility lsnrctl status on the database server machine.

    Also oci_connect does not throw an exception as far as i can tell so your try/catch is useless unless you planned on throwing your own when it returns false.

    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题