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 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题