PHP连接MSSQL Server无法获取数据排查求助
Hey there, let's break down the problems in your code and environment that are keeping you from pulling data into your calendar. I'll walk through each fix step by step:
1. Fix Variable Typos in Your Query Code
Your query script has several variable mismatches that are silently breaking the data fetching:
- You defined
$query_resultsbut tried to use$query_menu_resultsin the loop - You stored the fetched row in
$resultsbut tried to access$rowsinside the array $startand$endare undefined—you need to pull those values directly from your query results
Here's the corrected query code:
<?php // Enable error reporting to catch hidden issues error_reporting(E_ALL); ini_set('display_errors', 1); include_once("connect.php"); $DBO = new DBO(); $DBO->Connect(); $query = "SELECT ID, TYPE, STARTDATE, ENDDATE FROM TABLENAME"; $query_results = $DBO->RunSql($query) or die('Error in query: ' . mssql_get_last_message()); $calendar = array(); while( $results = mssql_fetch_assoc($query_results) ) { $calendar[] = array( 'ID' => $results['ID'], 'TYPE' => $results['TYPE'], 'url' => "#", 'class' => 'event-important', 'start' => $results['STARTDATE'], 'end' => $results['ENDDATE'] ); } $calendarData = array( "success" => 1, "result" => $calendar ); echo json_encode($calendarData); exit; ?>
2. Replace Deprecated MSSQL Functions
The mssql_* functions you're using were removed in PHP 7.0. If your WAMP setup uses PHP 7 or newer (which most modern versions do), these functions won't work at all—this is likely a core issue.
Instead, use the official sqlsrv extension for MSSQL:
- First, confirm the
sqlsrvextension is enabled in your WAMP PHP setup (check viaphpinfo()). - Update your
connect.phpto usesqlsrvfunctions:
<?php class DBO { private $server = "servername"; // Example: "localhost\SQLEXPRESS" for local MSSQL private $db_name = "dbname"; private $password = "pass"; private $username = "username"; private $conn; function Connect() { $connectionInfo = array( "Database" => $this->db_name, "UID" => $this->username, "PWD" => $this->password ); $this->conn = sqlsrv_connect($this->server, $connectionInfo); if (!$this->conn) { die(print_r(sqlsrv_errors(), true)); } } function RunSql($sqlQuery) { return sqlsrv_query($this->conn, $sqlQuery); } } ?>
If you stick with older PHP (not recommended), ensure the mssql extension is enabled in your php.ini.
3. Enable Error Reporting to Catch Hidden Issues
You mentioned no error messages are showing up—this is because PHP's error reporting is likely disabled. Adding the lines below at the top of your query script will reveal critical errors like missing extensions or invalid credentials:
error_reporting(E_ALL); ini_set('display_errors', 1);
4. Verify MSSQL Connection Details
Double-check these critical settings:
- Server Name: For local MSSQL, this is often
localhost\SQLEXPRESS(not just "servername") - Credentials: Ensure your username/password has read access to the
TABLENAMEtable - Firewall: Make sure your WAMP server is allowed to connect to the MSSQL port (default 1433)
- Database Name: Confirm
dbnamematches the exact name of your MSSQL database
Once you apply these fixes, your calendar should start pulling data from MSSQL correctly.
内容的提问来源于stack exchange,提问作者Nuisance

