0

我在从 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()的地方。

谢谢大家的帮助!

4

1 回答 1

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);

之类的

于 2012-11-12T09:41:39.967 回答