msphpsql
AGENTS.md1.9k starsChanged 8 months agoMITView on GitHub
This document provides guidance for AI coding agents working with the msphpsql GitHub repository — the open-source home of the sqlsrv and **pdo_sqlsrv** PHP extensions for Microsoft SQL Server. The drivers ship as two separate PHP extensions that share a common C++ core layer:
Trending
Quiet since May.about the whole repository
Who's talking about microsoft/msphpsql
- microsoft/homebrew-mssql-release“odbcinst: SQLInstallDriverEx failed with Invalid install path.”GitHub · Jul 31, 2017 · 12 reactions
- laradock/laradock“Unable to build php-fpm with mssql enabled”GitHub · Mar 1, 2023 · 8 reactions
- php/php-src“An error occurs in set_attribute when using PDO SQLSRV”GitHub · Aug 23, 2023 · 5 reactions
- laravel/framework“Disabling SQL Server binary-encoding of uniqueidentifier columns”GitHub · May 15, 2018 · 5 reactions
- mkleehammer/pyodbc“pyodbc.OperationalError: ('08001', '[08001] [Microsoft][ODBC Driver 17 for SQL Server]TCP Provider: Error …”GitHub · Aug 20, 2019 · 4 reactions
The biggest 5 of 16 mentions linking to the repository that our crawl found. Each line is their own headline.
See all 16
Talked about on GitHub (16 times).
On GitHub · 16
- Jul 31, 2017odbcinst: SQLInstallDriverEx failed with Invalid install path. — microsoft/homebrew-mssql-release12 reactions · 28 comments
- Mar 1, 2023Unable to build php-fpm with mssql enabled — laradock/laradock8 reactions · 8 comments
- Aug 23, 2023An error occurs in set_attribute when using PDO SQLSRV — php/php-src5 reactions · 4 comments
- May 15, 2018Disabling SQL Server binary-encoding of uniqueidentifier columns — laravel/framework5 reactions · 11 comments
- Aug 20, 2019pyodbc.OperationalError: ('08001', '[08001] [Microsoft][ODBC Driver 17 for SQL Server]TCP Provider: Error code 0x2746 (10054) (SQLDriverConnect)') — mkleehammer/pyodbc4 reactions · 79 comments
11 more on GitHub
- Aug 24, 2023[10.x] Ignore MSSQL exception about PDO::ATTR_STRINGIFY_FETCHES — laravel/framework3 reactions · 23 comments
- Dec 8, 2024[Package Request] - sqlsrv - Microsoft Drivers for PHP for SQL Server — amazonlinux/amazon-linux-20232 reactions · 1 comment
- Feb 15, 2023unixodbc_dev package is missing unixodbc.h — lurcher/unixODBC2 reactions · 3 comments
- Feb 28, 2026Seems to fail with new PECL release os sqlsrv 5.13.0 — ddev/ddev-sqlsrv1 reaction
- May 25, 2022pdo_sqlsrv version update[Feature]: — cytopia/devilbox1 reaction · 2 comments
- Jul 4, 2020Remove workaround in PDOSqlsrv\Connection::quote() — doctrine/dbal1 reaction · 1 comment
- Feb 14, 2018DB-mismatch on MSSQL Server — SuiteCRM/SuiteCRM1 reaction · 1 comment
- May 27, 2026php8*Extensions.{pdo_sqlsrv,sqlsrv}: 5.10.1/5.12.0 -> 5.13.1 — NixOS/nixpkgs0 reactions
- Feb 3, 2025Rephrase that following generic PDO attributes may not be implemented — php/doc-en0 reactions · 1 comment
- Feb 3, 20215.9.0 for PHP Driver for SQL Server — orioh2/Asset0 reactions
- Aug 8, 2018安装SQL Server 2008 R2需要注意的事项 — lilyomo/guides0 reactions
Where files are headingNew AGENTS.md files have more rules about keeping secrets out of the code, and fewer about checking a change actually works.
Of the AGENTS.md files we read, the 11,773 first seen in the last six months, against the older ones: the kind of rule that gained most share, and the kind that lost most. About files like this in general, not this one, and an estimate, since a kind is a pattern in the words.
The file
Show its rules and text
We split this AGENTS.md into the rules it gives an agent
24 rules. About 0 are common, 8 are distinctive, and 16 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈8
Distinctive · estimate
This author's own choices. Worth a look.
16
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 80
Zend API integration: All C++ code uses the PHP Zend Engine API.
- Distinctive: line 82
ODBC layer: All SQL Server communication goes through the Microsoft ODBC Driver for SQL Server (version 17 or 18).
- Very specific: line 119
All version information lives in source/shared/version.h:
- Very specific: line 133
When PREVIEW > 0, the version string becomes MAJOR.MINOR.PATCH-betaN (e.g., 5.13.0-beta1)
- Distinctive: line 208
Data types: All SQL Server types, Unicode, large objects, date/time, decimal precision
- Distinctive: line 210
Transactions: Commit, rollback, savepoints
- Distinctive: line 220
ODBC Abstraction: All database communication uses ODBC.
- Very specific: line 222
PHP Zend Compatibility: Follow Zend Engine API conventions.
- Distinctive: line 224
Backward Compatibility: Avoid breaking existing PHP userland code.
- Very specific: line 234
Add or update .phpt tests in both test/functional/sqlsrv/ and test/functional/pdo_sqlsrv/ if shared code changed
- Very specific: line 235
Update CHANGELOG.md with a description under the appropriate version heading
- Very specific: line 239
Add the ODBC keyword mapping in source/shared/core_sqlsrv.h (connection option definitions)
- Distinctive: line 242
Default to backward-compatible value
- Distinctive: line 243
Add tests for the new option in both test suites
- Very specific: line 249
Update SQLVERSION_MAJOR, SQLVERSION_MINOR, and/or SQLVERSION_PATCH
- Very specific: line 250
Update PHP_SQLSRV_VERSION and PHP_PDO_SQLSRV_VERSION string literals
- Very specific: line 252
Update CHANGELOG.md with new version heading and release notes
- Very specific: line 257
Create a .phpt file in the appropriate test/functional/ subdirectory
- Very specific: line 259
Use MsCommon.inc helpers for connection setup and data generation
- Distinctive: line 260
Ensure the test is deterministic and cleans up after itself (drop temp tables, close connections)
- Very specific: line 266
Run source/packagize.sh to copy shared/ into both extension directories
- Very specific: line 267
Verify PHP_SQLSRV_VERSION / PHP_PDO_SQLSRV_VERSION strings have no hyphens
- Very specific: line 307
Check test/functional/ for usage patterns and expected behavior
- Very specific: line 308
Read MsCommon.inc for available test helper functions
≈ “Common” is an estimate: at least 1 in 5 of the 16,595 AGENTS.md files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
AI Agent Guidelines for Microsoft Drivers for PHP for SQL Server
This document provides guidance for AI coding agents working with the msphpsql GitHub repository — the open-source home of the sqlsrv and pdo_sqlsrv PHP extensions for Microsoft SQL Server.
Quick Start
Essential Context Files
| File | Purpose |
| README.md | Project overview, prerequisites, build basics |
| CHANGELOG.md | Full release history; current version at top |
| Linux-mac-install.md | Step-by-step install guide for every supported Unix platform |
| buildscripts/README.md | Detailed Windows build instructions (manual & scripted) |
| source/shared/version.h | Single source of truth for version numbers and preview state |
Repository at a Glance
| Area | Path | Description |
| Shared core | source/shared/ | C++ code shared by both extensions |
| SQLSRV extension | source/sqlsrv/ | Procedural PHP API extension |
| PDO_SQLSRV extension | source/pdo_sqlsrv/ | PDO interface extension |
| Packaging script | source/packagize.sh | Copies shared/ into each extension dir for PECL packaging |
| Functional tests | test/functional/ | .phpt test suites for both extensions |
| Extended tests | test/extended/ | Always Encrypted v2 special tests |
| BVT tests | test/bvt/ | Basic verification tests |
| Performance tests | test/Performance/ | Benchmark suite (phpbench) |
| Windows build scripts | buildscripts/ | builddrivers.py, buildtools.py |
| Samples | sample/ | sqlsrv_sample.php, pdo_sqlsrv_sample.php |
| CI (Linux/Mac) | azure-pipelines.yml | Azure Pipelines — builds PHP from source, runs tests |
| CI (Windows) | appveyor.yml | AppVeyor — PHP SDK build matrix |
| Docker | Dockerfile-msphpsql | Ubuntu 24.04 dev environment image |
Architecture
Two Extensions, One Shared Core
The drivers ship as two separate PHP extensions that share a common C++ core layer:
source/
├── shared/ # Core logic (ODBC, connection, statement, stream, error handling)
│ ├── core_conn.cpp # Connection management
│ ├── core_init.cpp # Driver initialization
│ ├── core_results.cpp # Result set processing
│ ├── core_stmt.cpp # Statement execution
│ ├── core_stream.cpp # Stream handling
│ ├── core_util.cpp # Utility functions
│ ├── core_sqlsrv.h # Main shared header (includes Zend API, ODBC, platform abstractions)
│ ├── version.h # Version constants
│ ├── FormattedPrint.cpp/h # Cross-platform printf implementation
│ ├── StringFunctions.cpp/h # Cross-platform string operations
│ ├── localizationimpl.cpp # Localization/encoding support
│ ├── msodbcsql.h # ODBC driver header
│ └── xplat_*.h, typedefs_for_linux.h, interlockedatomic*.h # Platform abstractions
│
├── sqlsrv/ # Procedural API extension
│ ├── conn.cpp # sqlsrv_connect(), sqlsrv_close(), etc.
│ ├── init.cpp # Extension module init/shutdown
│ ├── stmt.cpp # sqlsrv_query(), sqlsrv_fetch(), etc.
│ ├── util.cpp # Error handling, logging
│ ├── config.m4 # Linux/Mac autoconf build
│ └── config.w32 # Windows JScript build
│
├── pdo_sqlsrv/ # PDO extension
│ ├── pdo_dbh.cpp # PDO database handle (new PDO(...))
│ ├── pdo_init.cpp # Extension module init/shutdown
│ ├── pdo_parser.cpp # SQL parser for named params
│ ├── pdo_stmt.cpp # PDOStatement operations
│ ├── pdo_util.cpp # Error handling
│ ├── config.m4 # Linux/Mac autoconf build
│ └── config.w32 # Windows JScript build
│
└── packagize.sh # Copies shared/ into sqlsrv/ and pdo_sqlsrv/ for PECLKey Design Patterns
- Zend API integration: All C++ code uses the PHP Zend Engine API. The shared header
core_sqlsrv.hincludes<php.h>and<zend.h>with MSVC warning suppression for known benign warnings (4100, 4127, 4146, 4244, 4267, 4456, 4457, 4706). - Platform abstraction: Unix builds use
typedefs_for_linux.h,xplat_*.h, andinterlockedatomic*.hto map Win32 types/functions to POSIX equivalents (e.g.,stricmp→strcasecmp,GetLastError→errno). - ODBC layer: All SQL Server communication goes through the Microsoft ODBC Driver for SQL Server (version 17 or 18). The code never talks to TDS directly.
- MARS: Multiple Active Result Sets enabled by default via
MARS_Connection={Yes}.
Build System
Linux / macOS (autoconf)
Each extension has a config.m4 that registers the extension with PHP's build system:
# Build from PHP source tree (extensions in ext/ directory)
phpize
./configure --with-sqlsrv # or --with-pdo_sqlsrv
make
make installThe config.m4 includes logic to avoid double-compiling shared sources when both extensions are built as static (non-shared) in the same PHP build.
Windows (PHP SDK + JScript)
Each extension has a config.w32 (JScript) for the PHP Windows SDK build:
- Visual Studio: VS2019 for PHP 8.3, VS2022 for PHP 8.4+
- Security flags:
/NXCOMPAT,/DYNAMICBASE,/guard:cf,/ZH:SHA_256,/W4,/WX,/CETCOMPAT,/Qspectre(for VCVERS >= 1913) - Scripted builds:
buildscripts/builddrivers.pyautomates downloading PHP SDK, source, and building both extensions
PECL Packaging
source/packagize.sh copies shared/ files into both sqlsrv/ and pdo_sqlsrv/ directories, making each extension self-contained for PECL distribution. Note: PECL version strings must not contain hyphens (e.g., 5.13.0beta1 not 5.13.0-beta1).
Docker
The Dockerfile-msphpsql provides a complete Ubuntu 24.04 development environment with PHP, ODBC 18, and all build dependencies pre-installed.
Version Management
All version information lives in source/shared/version.h:
| Constant | Purpose | Current |
SQLVERSION_MAJOR | Incompatible API changes | 5 |
SQLVERSION_MINOR | Backward-compatible features | 13 |
SQLVERSION_PATCH | Backward-compatible bug fixes | 0 |
SQLVERSION_BUILD | Build metadata (appended as +N) | 0 |
PREVIEW | Set to 1+ for beta releases, 0 for stable | 0 |
PHP_SQLSRV_VERSION | PECL version string for sqlsrv | "5.13.0" |
PHP_PDO_SQLSRV_VERSION | PECL version string for pdo_sqlsrv | "5.13.0" |
Versioning rules:
- Follows semantic versioning
- When
PREVIEW > 0, the version string becomesMAJOR.MINOR.PATCH-betaN(e.g.,5.13.0-beta1) - PECL versions use
MAJOR.MINOR.PATCHbetaN(no hyphen) - The
PHP_SQLSRV_VERSIONandPHP_PDO_SQLSRV_VERSIONconstants must be updated manually (cannot use preprocessor macros due to PECL/Pickle constraints)
Testing
Test Format
Tests use PHP's standard .phpt format:
--TEST--
Description of the test
--SKIPIF--
<?php require('skipif_*.inc'); ?>
--FILE--
<?php
// Test code here
?>
--EXPECT--
Expected outputTest Organization
| Directory | Contents | Count |
test/functional/sqlsrv/ | SQLSRV extension tests | ~260+ tests |
test/functional/pdo_sqlsrv/ | PDO_SQLSRV extension tests | ~280+ tests |
test/functional/setup/ | SQL scripts, BCP data files, Python setup scripts | |
test/functional/inc/ | Shared binary data (GIF files), TVP test data | |
test/extended/ | Always Encrypted v2 tests | ~12 tests |
test/bvt/ | Basic verification tests | |
test/Performance/ | phpbench-based benchmarks |
Test Configuration
Tests read connection info from MsSetup.inc (present in each test directory). Connection settings can be overridden via environment variables:
| Variable | Purpose | Default |
MSSQL_SERVER | SQL Server hostname | TARGET_SERVER placeholder |
MSSQL_DATABASE_NAME | Database name | TARGET_DATABASE placeholder |
MSSQL_UID | SQL login username | TARGET_USERNAME placeholder |
MSSQL_PWD | SQL login password | TARGET_PASSWORD placeholder |
MSSQL_DRIVER | ODBC driver version string | ODBC Driver 18 for SQL Server |
Running Tests
# Run all sqlsrv tests
php run-tests.php test/functional/sqlsrv/
# Run all pdo_sqlsrv tests
php run-tests.php test/functional/pdo_sqlsrv/
# Run a single test
php run-tests.php test/functional/sqlsrv/sqlsrv_connect.phptTest Helper Includes
| File | Purpose |
MsSetup.inc | Connection configuration (server, database, credentials, ODBC driver) |
MsCommon.inc | ~600 lines of shared helpers: connect(), isWindows(), testMode(), traceMode(), table creation, data generators |
MsHelper.inc | Additional helper functions |
MsData.inc | Test data constants |
skipif*.inc | Skip conditions (OS, extensions, features, server version) |
tools.inc | Diagnostic and utility functions |
Test Categories
Tests cover a wide range of features:
- Connectivity: Connect, close, connection options, connection resiliency, connection pooling
- Data types: All SQL Server types, Unicode, large objects, date/time, decimal precision
- Fetch modes: Forward-only, scrollable, buffered queries, client-side cursors
- Transactions: Commit, rollback, savepoints
- Stored procedures: Input/output params, return values, result sets
- Always Encrypted: Column encryption, key vault, enclave operations
- Azure AD / Entra: Authentication modes (
ActiveDirectoryPassword,ActiveDirectoryMSI, etc.) - Table-Valued Parameters (TVP): Sending structured data
- Bulk Copy (BCP):
test/functional/setup/*.datand*.fmtfiles
Core Principles
- Shared Core, Separate Extensions: Common logic goes in
source/shared/. Extension-specific API binding goes insource/sqlsrv/orsource/pdo_sqlsrv/. Never duplicate logic between extensions. - ODBC Abstraction: All database communication uses ODBC. Never implement protocol-level (TDS) communication.
- Cross-Platform: Code must compile and run on Windows, Linux, and macOS. Use the platform abstraction headers (
xplat_*.h,typedefs_for_linux.h) for OS-specific functionality. - PHP Zend Compatibility: Follow Zend Engine API conventions. Use
zend_*functions for memory management. Be aware of thread-safety requirements (ZTS vs NTS builds). - Security by Default: Windows builds enforce
/NXCOMPAT,/DYNAMICBASE,/guard:cf,/Qspectre,/CETCOMPAT. Never log credentials or connection strings in error output. - Backward Compatibility: Avoid breaking existing PHP userland code. New connection options should default to preserving existing behavior.
- Test Coverage: All changes must include
.phpttests. Bothsqlsrvandpdo_sqlsrvshould be tested when shared code changes. Tests have standard dependencies connectivity to a server and database. If a test includes dependencies beyond the standard that aren't present, they should include relevant skip logic.
Common Tasks
Bug Fix Workflow
- Reproduce the issue — write or identify a
.phpttest that demonstrates the bug - Determine if the fix belongs in
source/shared/(affects both extensions) or in an extension-specific file - Implement the fix
- Add or update
.phpttests in bothtest/functional/sqlsrv/andtest/functional/pdo_sqlsrv/if shared code changed - Update
CHANGELOG.mdwith a description under the appropriate version heading
Adding a New Connection Option
- Add the ODBC keyword mapping in
source/shared/core_sqlsrv.h(connection option definitions) - Register the option in
source/shared/core_conn.cppconnection option arrays - Expose in
source/sqlsrv/conn.cppand/orsource/pdo_sqlsrv/pdo_dbh.cpp - Default to backward-compatible value
- Add tests for the new option in both test suites
- Document in
CHANGELOG.md
Version Bump
- Edit
source/shared/version.h:- Update
SQLVERSION_MAJOR,SQLVERSION_MINOR, and/orSQLVERSION_PATCH - Update
PHP_SQLSRV_VERSIONandPHP_PDO_SQLSRV_VERSIONstring literals - Set
PREVIEWto 0 for stable, or 1+ for beta releases
- Update
- Update
CHANGELOG.mdwith new version heading and release notes - The file header comment
Microsoft Drivers X.YY for PHP for SQL Serverin source files may need updating for major/minor bumps
Adding a New Test
- Create a
.phptfile in the appropriatetest/functional/subdirectory - Include standard skip logic:
--SKIPIF--with<?php require('skipif_*.inc'); ?> - Use
MsCommon.inchelpers for connection setup and data generation - Ensure the test is deterministic and cleans up after itself (drop temp tables, close connections)
- Provide explicit
--EXPECT--or--EXPECTF--output
Preparing a PECL Release
- Set version in
source/shared/version.h(ensurePREVIEWis set correctly) - Run
source/packagize.shto copyshared/into both extension directories - Verify
PHP_SQLSRV_VERSION/PHP_PDO_SQLSRV_VERSIONstrings have no hyphens - Each extension directory becomes a self-contained PECL package
CI / CD
GitHub CI
| Pipeline | Platform | Trigger |
Azure Pipelines (azure-pipelines.yml) | Linux, macOS | Push to dev, fix/*; PRs to dev |
AppVeyor (appveyor.yml) | Windows | Push (except legacy branches) |
Codecov (codecov.yml) | Windows | Coverage reporting |
The Azure Pipelines job builds PHP from source on each target platform, compiles both extensions, then runs the full .phpt test suite against a SQL Server (Docker container on Linux, local instance on Windows/macOS).
Internal CI/CD (msphpsql in ADO)
The internal Azure DevOps repo (msphpsql) contains the production CI/CD pipelines for official builds, matrix testing, signing, packaging, and release. See that repo's AGENTS.md for pipeline architecture details.
Branching and Contributions
dev— active development branch; all PRs should targetdevmain(ormaster) — stable release branchfix/*— bug fix branches (trigger CI)- Pull requests require CI to pass before merge
External Resources
| Resource | Link |
| Microsoft Docs (PHP Drivers) | https://learn.microsoft.com/sql/connect/php/microsoft-php-driver-for-sql-server |
| ODBC Driver Docs | https://learn.microsoft.com/sql/connect/odbc/microsoft-odbc-driver-for-sql-server |
| PHP Internals (Building) | https://wiki.php.net/internals/windows/stepbystepbuild_sdk_2 |
| PECL sqlsrv | https://pecl.php.net/package/sqlsrv |
| PECL pdo_sqlsrv | https://pecl.php.net/package/pdo_sqlsrv |
| GitHub Issues | https://github.com/microsoft/msphpsql/issues |
| GitHub Releases | https://github.com/microsoft/msphpsql/releases |
Getting Help
- Check
test/functional/for usage patterns and expected behavior - Read
MsCommon.incfor available test helper functions - Consult
core_sqlsrv.hfor internal data structures and ODBC wrappers - For ODBC behavior, reference the Microsoft ODBC Driver documentation
- For PHP extension API, reference the PHP Internals Book
This document is automatically loaded as context for AI agents working in this repository.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

