Home Backend Development PHP Tutorial PHP connection to mssql About PHP connection to mssql: pdo odbc sql server

PHP connection to mssql About PHP connection to mssql: pdo odbc sql server

Jul 29, 2016 am 08:46 AM

There is only one php_pdo_odbc.dll.
so~The latest and best php method to connect to mssql should be like this:

Copy the code The code is as follows:


$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($a);
foreach ($a as $b) {
var_dump($b);
}
?>


About the problem that PHP cannot connect to the MSSQL database
A new server was configured today, and the configuration is IIS +php, when running, it was found that there was an error when PHP connected to the remote mssql database. The error code is as follows:
Warning: mssql_connect(): Unable to connect to server:
Think about it, it was no problem before, what happened? Later, I searched online and found an article, only to find out that the server needs to install mssql in order to use php to connect to mssql. Originally, I didn’t need to use mssql on the new server, but now I have no choice but to install it. Yes, after installing mssql there will be no problem.
I was thinking, what would happen if it is apache+php on Linux? It is impossible to install mssql, haha, I am dizzy.
The following is an article I found.
php configuration:
Set as follows in the php.ini file, find
;extension=php_mssql.dll and remove the semicolon in front of it
Find extension_dir = d:extension
Your php.ini may not contain d:extension
Change it to The path of php_mssql.dll under the extensions directory under the php installation directory. If you have not moved it to another place (assuming that your php installation path is d:php)
Change it to extensi
Then restart the web server
This is easy to do, but after completing such settings, it still cannot be connected. The error message is as follows:
MS SQL Server database connection error! Please check whether the database host variable settings are correct!!!
And the host variable settings I checked over and over again, and there was no problem with those settings. After browsing the web, I found the following clues:
php.com information:
I am trying to connect to SQL Server 2000 from PHP
I bumped to following warning:
Warning: mssql_connect(): Unable to connect to server: SERVERPortal
..... on line 5
on line 5 there is:
$db_connect = mssql_connect('SERVERPortal', '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 hours ,I found the problem was caused by
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 could 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, the trouble was ntwdblib.dll
ntwdblib.dll’s main function is to provide sql server connection service.
The PHP version I use is 4.3.9. In the windows/system32/ of the server where it is installed, I found that the version of the ntwdblib.dll file is 2000.2.8.0. This version supports SQL Server 7.0, because when installing PHP All files under dlls will be overwritten into the system directory, so when I use it to connect to sql server 2000, of course I will be unable to connect.
Later, I found out that the version of ntwdblib.dll was 2000.80.2039.0 on a server where SQL Server 2000 was installed normally. I copied this file to overwrite the previous version. After restarting the server, everything was normal.
Supplement: If the database name starts with a number, it will also prompt that it cannot be opened. At this time, it is very simple. Enclose the name of the database in square brackets [ ]
and it will be done. For example, if 123bbs is rewritten as [123bbs], it will not work. Problem. In addition, this situation will also occur if your database name conflicts with the reserved words in SQL Server. You can also solve it by using square brackets.
In the end, the problem of PHP being unable to connect to SQL Server 2000 correctly was finally solved. Although it took most of a day, the harvest was still great. Posting it now will save brothers who encounter the same problem from taking some detours.
The above introduces the PHP connection to mssql. About PHP connection to mssql: pdo odbc sql server, including the content of PHP connection to mssql. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

See all articles