无法从php连接到oracle数据库

时间:2012-11-12 08:47:55

标签: php oracle database-connection

我在从php连接到mydatabase时遇到问题。我可以通过我的C#代码连接没有问题:

    private void test()  
    {
        string connect = "Driver={Microsoft ODBC for Oracle};CONNECTSTRING=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=XE)));Uid=user;Pwd=pass";
        string queryString = "DELETE FROM myTabel";

        using (OdbcConnection connection = new OdbcConnection(connect))
        {
            try
            {
                OdbcCommand command = new OdbcCommand(queryString, connection);
                command.Connection.Open();
                command.ExecuteNonQuery();
                connection.Close();
                log.Debug("Lokalni podatki pobrisani!");
            }
            catch (Exception e)
            {
                Console.WriteLine("ne dela" + e.ToString());
                deluje = false;
            }
        }
    }

并且真的删除了所有数据,因为我检查了oracle SQL Developer,我连接到没有问题。 但是当我尝试运行该代码时:

          $tnsname = '(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
        (CONNECT_DATA = (SERVICE_NAME = XE)))';
    }

    $db['default']['hostname'] = $tnsname;
     $db['default']['username'] = "esz";
      $db['default']['password'] = "test";
      $db['default']['database'] = "esz";
     $db['default']['dbdriver'] = "oci8";
     $db['default']['dbprefix'] = "";
      $db['default']['pconnect'] = FALSE;
     $db['default']['db_debug'] = TRUE;
     $db['default']['cache_on'] = FALSE;
     $db['default']['cachedir'] = "";
     $db['default']['char_set'] = "utf8";
     $db['default']['dbcollat'] = "utf8_general_ci"; 

  echo 'Trying to connect to database: ' .$db['default']['database'];
  $dbh=mysql_connect( $db['default['hostname'],$db['default'['username'],$db['default']['password'])
   or die('Cannot connect to the database because: ' . mysql_error());

我收到了这些错误:

 mysql_connect() [function.mysql-connect]: php_network_getaddresses: getaddrinfo failed: No such host is known. 
 mysql_connect() [function.mysql-connect]: [2002] php_network_getaddresses: getaddrinfo failed: No such host is kn (trying to connect via tcp://(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521)) (CONNECT_DATA = (SERVICE_NAME = XE))):3306)
 php_network_getaddresses: getaddrinfo failed: No such host is known.

我试图超越mysql_connect()。

感谢大家的帮助!

1 个答案:

答案 0 :(得分:0)

使用oci_connect,如下所示:

$user = ...;
$password = ...;
$url = '//dbserver:1521/dbname';
$connection = oci_connect ($user, $password, $url);
...
$query = '...';
$stid = oci_parse ($connection, $query);
oci_execute ($stid);
oci_fetch ($stid);

之类的