Skip to content

odbc_pconnect fails with DSN and username/password with special characters #12251

Closed
@kevinreniers

Description

@kevinreniers

Description

Hello, we're trying to upgrade from PHP 8.1 to PHP 8.2.10. We integrate with an MSSQL server using FreeTDS/unixODBC and ext/odbc. After upgrading, the same code that worked on PHP 8.1 now fails on PHP 8.2.

I assume that Quote when appending username and password to the ODBC connection string is the cause of my issue.

The following code:

<?php

namespace App\Connections;

use RuntimeException;

class OdbcConnection implements DatabaseConnectionInterface
{
    private $connection;

    public function __construct(private readonly string $dsn, private readonly string $username, private readonly string $password)
    {
    }

    /**
     * @throws RuntimeException
     */
    public function connect(): void
    {
        $this->connection = odbc_pconnect($this->dsn, $this->username, $this->password);
    }
}

$conn = new OdbcConnection('Driver=FreeTDS;Server=myserver.local;Port=1433;Database=MyDatabase', 'user', 'foo(bar(');
$conn->connect();

Resulted in this output:

In OdbcConnection.php line 20:

  [ErrorException]
  Warning: odbc_pconnect(): SQL error: [FreeTDS][SQL Server]Unable to connect to data source, SQL state S1000 in SQLConnect

But I expected this output instead:

No error, works fine in PHP 8.1.

I can workaround the issue in my local development setup by creating an entry in /etc/odbc.ini and then only passing the section name to the $dsn parrameter of odbc_pconnect like so:

# /etc/odbc.ini
[sv1]
Driver=FreeTDS
Server=myserver.local
Port=1433
Database=MyDatabase
<?php

odbc_pconnect('sv1', 'user', 'foo(bar(');

Ideally, it would help me the most if I can make it work as-is without any workaround. It's not practical (but not impossible) to apply the /etc/odbc.ini workaround in my infrastructure. It would delay our upgrade significantly though.

Thank you in advance!

PHP Version

PHP 8.2.10

Operating System

RHEL 8.8, Debian Bullseye

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions