Skip to content

ODBC's heuristic for guessing if DSN or connection string could be better #8345

Closed
@NattyNarwhal

Description

@NattyNarwhal

Description

The following code:

<?php

$conn = odbc_connect("DSN=*LOCAL", null, null);
var_dump($conn);

Resulted in this output:

bool(true)

But I expected this output instead:

PHP Warning:  odbc_connect(): SQL error: [unixODBC][Driver Manager]Data source name not found and no default driver specified, SQL state IM002 in SQLConnect in /home/calvin/src/test-heuristic.php on line 3
bool(false)

The ODBC drivers use strchr(db/dsn, ";") (well, procedural uses strstr, but same deal) to detect if the string the user passed into the constructor is a connection string or not. However, this isn't accurate for connection strings with a single entity (i.e. a DSN=dsn - seems useless versus just using the DSN name, but valid and still useful if you're i.e. building a connection string programatically).

One solution might be to use the ODBC quoting rules like in my PR #8307, but there's valid DSNs that use the characters that Microsoft claims have to require quoting. For example, IBM's Db2 for i driver installs a *LOCAL DSN in odbc.ini (on IBM i versions of the driver for loopback, but the point remains, can i.e. easily just add a DSN of that name to your own system), which would make the simple DSN name case invalid for that DSN.

Another solution is to just scan for a subset of those characters - = might be enough to add, or instead of ;.

PHP Version

8.2.0-dev

Operating System

Fedora 35

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