Php connection mssql: pdo odbc SQL server

Source: Internet
Author: User

There is only one php_pdo_odbc.dll.
So ~ The latest and best php Method for connecting to mssql is as follows:
Copy codeThe Code is as follows:
<? Php
$ Cnx = new PDO ("odbc: Driver = {SQL Server}; Server = 127.0.0.1; Database = test;", 'sa ', 'asd123 ');
Var_dump ($ cnx );
$ A = $ cnx-> query ("SELECT * FROM [user]");
Var_dump ($ );
Foreach ($ a as $ B ){
Var_dump ($ B );
}
?>


The problem that PHP cannot connect to the MSSQL database
Today, a new server is configured with IIS + php. when running the result, an error occurred while connecting php to the remote mssql database. The error code is as follows:
Warning: mssql_connect (): Unable to connect to server:
I think it was okay before. Why? Then I went to the Internet to search and found an article, and found that the original server had to install mssql to connect to mssql using php. I didn't need mssql on the new server, but now there is no way, you can only install it. After mssql is installed, there will be no problem.
I was wondering what would happen to apache + php on linux, but it was impossible to install mssql.
The following is an article.
Php configuration:
Set the following settings in the php. ini file and find
; Extension = php_mssql.dll remove the semicolon
Find extension_dir = d: \ extension \
Your php. ini may not contain d: \ extension \
Change the path of php_mssql.dll under the extensions directory under the php installation directory. If you haven't moved it to another place (assume that your php installation path is d: \ php)
Change to extension_dir = d: \ php \ extensions \
Then restart the web server.

This is easy to do, but it cannot be connected after such a setting. The error message is as follows:
Ms SQL Server database connection error! Check whether the Database Host variable settings are correct !!!
I checked the host's variable settings over and over again, but those settings were none of the problem at all. I searched the webpage and found the following clues:

Php.com documents:
I am trying to connect to SQL Server 2000 from PHP
I bumped to following warning:
Warning: mssql_connect (): Unable to connect to server: SERVER \ Portal
... On line 5
On line 5 there is:
$ Db_connect = mssql_connect ('server \ Portal ', 'sa', 'My _ passwd ');
I did the following
1. enabled php_mssql.dll extension in PHP. ini
2. every dll is in proper place (System32 or PHP folder), including ntwdblib. dll
I search lots of profile throught web, but no one give me proper answer to resolve it.
After a few hour, I found the problem was caused
Ntwdblib. dll, which version is 7.00.839, when I replaced old ntwdblib. dll with the new
Ntwdblib. dll, which version is 8.00.194, all problem are solved.
We had some, read a lot, of problems with MSSQL under Windows 2003.
We had 2 the same windows, php, php-ini, everything machines but only one cocould connect.
Unable to connect was the error message.
Finnaly we checked the version of ntwdblib. dll and the one distributed with PHP was 7. 00 ....
And the version of the one on the SQL Server install was 8. 00... so we copied this one in
The php and apache dir and it worked.
The problem was found in this way, and the problem was caused by ntwdblib. dll.
Ntwdblib. dll is mainly used to provide the SQL server connection service.
My php version is 4.3.9. in the windows/system32/directory where the server is installed, I found ntwdblib. the dll file version is 2000.2.8.0. This version supports SQL server 7.0 because all the files in the dlls will overwrite the system when installing PHP.
Directory, so when I use it to connect to SQL server 2000, it will of course be unable to connect.
Later, I found ntwdblib on a server where SQL server 2000 is installed normally. the dll version is 2000.80.2039.0. I copied the file to overwrite the previous version. After the server is restarted, everything works normally.
Supplement: if the database name starts with a number, it will prompt that it cannot be opened. In this case, it is easy to use the database name in brackets [].
You can solve the problem by writing 123bbs to [123bbs]. In addition, if your database name conflicts with the reserved words in SQL server, you can solve the problem by using the brackets.
In the end, the failure of PHP to correctly connect to SQL server 2000 finally solved the problem. Although it took a long time, it still gained a lot. Now I have posted it, it also makes the same problem less detours.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.