catfan / Medoo

The lightweight PHP database framework to accelerate the development.
https://medoo.in
MIT License
4.84k stars 1.15k forks source link

[BUG] Connect SQL Server on a different platform #573

Closed huangenyan closed 6 years ago

huangenyan commented 7 years ago

Suppose I have a SQL Server installed on a Windows Server (IP: 10.0.0.1), and I want to connect the DB from an API server running on Linux (IP: 10.0.0.2):

new Medoo([
        'database_type' => 'mssql',
        'database_name' => 'XXX',
        'server' => '10.0.0.1',
        'username' => 'XXX',
        'password' => 'XXX'
    ]); 

The code above would use the driver dblib instead of sqlsrv because Medoo determines the driver to using according to the current OS with the following code:

case 'mssql':
if (strstr(PHP_OS, 'WIN'))
{
    $attr = [
        'driver' => 'sqlsrv',
        'Server' => $options[ 'server' ] . ($is_port ? ',' . $port : ''),
        'Database' => $options[ 'database_name' ]
    ];
}
else
{
    $attr = [
        'driver' => 'dblib',
        'host' => $options[ 'server' ] . ($is_port ? ':' . $port : ''),
        'dbname' => $options[ 'database_name' ]
    ];
}

This is obviously wrong because the driver to use should be determined by the DB server (10.0.0.1) instead of the current server (10.0.0.2).

To solve the problem, I suggest to use either sqlsrv or dblib when specify the database_type and just use the passing value in DSN string.

catfan commented 7 years ago

Good! Thank you for this suggestion. That's really a problem.

It may be fixed on next version.

catfan commented 6 years ago

It's now fixed. https://medoo.in/api/new