Try different odbc connect calls

2019-09-05 09:03发布

问题:

I would like to try to connect to a database (using odbc) where I necessarily don't know the exact password. That is, I have a couple of different alternatives what the password might be, and I want my code to figure out which one is right.

How can I do this using PHP?

回答1:

Just wrap the call to odbc_connect in a foreach loop trying all the passwords:

function my_odbc_connect($dsn, $user, array $passwords) {
    foreach ($passwords as $password) {
        $connection = odbc_connect($dsn, $user, $password);
        if (is_resource($connection)) {
            return $connection;
        }
    }
    return false;
}

and then just do

$connection = my_odbc_connect('blah', 'user', array('foo', 'bar', 'baz'));


标签: php odbc