You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP连接MSSQL Server无法获取数据排查求助

Troubleshooting Your MSSQL-PHP Calendar Data Issue

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_results but tried to use $query_menu_results in the loop
  • You stored the fetched row in $results but tried to access $rows inside the array
  • $start and $end are 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:

  1. First, confirm the sqlsrv extension is enabled in your WAMP PHP setup (check via phpinfo()).
  2. Update your connect.php to use sqlsrv functions:
<?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 TABLENAME table
  • Firewall: Make sure your WAMP server is allowed to connect to the MSSQL port (default 1433)
  • Database Name: Confirm dbname matches the exact name of your MSSQL database

Once you apply these fixes, your calendar should start pulling data from MSSQL correctly.

内容的提问来源于stack exchange,提问作者Nuisance

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 09:51:15