首页 > 解决方案 > 我无法使用 php 7 连接到 Ingres

问题描述

I have to build a php website which has to connect to an Ingres database server.

我已经下载了 sourceforge pjbs JDBC-PHP 桥。我将它安装在项目文件夹中并尝试连接到我的 ingres 数据库。我不明白 fsockopen() 需要哪些参数,对于数据库主机(VMS 服务器),我使用 vnode 但 ingres 使用字符串作为端口,而 fsockopen 想要一个整数。我在 HP OpenVMS v.8.4 上的 Windows 7 Ingres 9.2 上使用 php 7.2.3

<?php

require "lib/PJBridge.php";

function get_connection()
{
    $connStr = "jdbc:ingres://vnode:II7/dbname";

    $db = new PJBridge();
    $result = $db->connect($connStr, $user, $password);

    if(!$result){
        die("Failed to connect");
}
return $result;}


<?php
class PJBridge {

    private $sock;
    private $jdbc_enc;
    private $app_enc;

    public $last_search_length = 0;

    function __construct($host="vnode", $port=-1, $jdbc_enc="ascii", $app_enc="ascii") {

        $this->sock = fsockopen($host, $port);

        $this->jdbc_enc = $jdbc_enc;
        $this->app_enc = $app_enc;
}

标签: phpingres

解决方案


ingres uses a string as port- 然后将其转换为 int。您可能还应该验证它实际上返回了一个范围内的数字字符串(有效的端口范围,对于 ipv4 和 ipv6,是 1-65535。(端口 0 也存在,但在大多数系统上不可用))

$port = "9999";
$old = $port;
if (false === ($port = filter_var ( $port, FILTER_VALIDATE_INT, array (
        'options' => array (
                'min_range' => 1,
                'max_range' => 65535 
        ) 
) ))) {
    throw new \LogicException ( "port is not a valid integer in range 1-65535! ($old)" );
}
// here, $port is guaranteed to be an int in range 1-65535.

至于socket_connect想要什么选项,查看这个例子,在端口80上连接到example.com下载html网站:

$fp = fsockopen ( "www.example.com", 80, $errno, $errstr, 30 );
if (! $fp) {
    echo "error connecting: $errstr ($errno)<br />\n";
} else {
    $out = "GET / HTTP/1.1\r\n";
    $out .= "Host: www.example.com\r\n";
    $out .= "Connection: Close\r\n\r\n";
    fwrite ( $fp, $out );
    while ( ! feof ( $fp ) ) {
        echo fgets ( $fp, 128 );
    }
    fclose ( $fp );
}

全部取自fsockopen 文档。

最后一点,我没有使用 Ingres 的经验,但是原始 tcp api 变得非常罕见,你确定没有更高级别的 api 可用于 Ingres 吗?编辑:是的,检查http://php.net/manual/en/book.ingres.php,使用该扩展几乎肯定比使用原始 tcp 套接字容易得多


推荐阅读