问题
I'm trying to connect to a Microsoft SQL Server / Microsoft Azure database with PHP's PDO:
<?php
// no actual login data, but similar string lengths
$dbHost = 'aa1234bbb5.database.windows.net';
$dbUser = 'db_a1a1a1a1_b2b2_c3c3_d4d4_e5e5e5e5e5e5_ExternalWriter';
$dbPass = 'pPAs0wOoO1&r#dd';
$dbName = 'db_a1a1a1a1_b2b2_c3c3_d4d4_e5e5e5e5e5e5';
try {
$pdo = new PDO("dblib:host=$dbHost:1433;dbname=$dbName", $dbUser, $dbPass);
} catch (PDOException $e) {
echo "Failed to get DB handle: " . $e->getMessage() . "\n";
exit;
}
The PDO initialization throws an PDOException with the following message:
SQLSTATE[HY000] Name too long for LOGINREC field (severity 2)
I'm running PHP 5.4.41-0+deb7u1 on Debian 7.7 x64.
My questions boil down to:
- Why do I get this error message?
- How should I actually connect to the database?
Note: I cannot change the login data because I need to access the backend database of a Microsoft Access Web App. If you create such a Web App, Microsoft creates the database on one of its "publicly available" Azure servers. You can ask the server to give you a username and a password - but unfortunately you have to use whatever is given to you.
回答1:
"LOGINREC" structure can be a maximum of 30 characters. You'll have to shorten long strings.
回答2:
Have you tried SqlSrv - an alternative driver for MS SQL from Microsoft. Per my understanding, PDO_DBLIB - extension: http://php.net/manual/en/ref.pdo-dblib.php is not available anymore on Windows with PHP 5.3 or later. It's recommended to use SqlSrv and I have tested your long dbUser & dbPass, it works fine on my side via using SqlSrv:
Please feel free to let me know if I have any misunderstood on your issue.
Edit:
I have tried odbc_connect on my side as well and it works fine
For more information about Microsoft SQL Server ODBC driver for Linux, you can refer to http://www.codesynthesis.com/~boris/blog/2011/12/02/microsoft-sql-server-odbc-driver-linux/ & http://www.easysoft.com/developer/languages/php/sql_server_unix_tutorial.html#driver and it is not too difficult to install it on 64-bit Debian or Ubuntu.
来源:https://stackoverflow.com/questions/31004804/how-to-use-phps-dblib-pdo-driver-with-long-usernames-sqlstatehy000-name-to