简体   繁体   中英

php script stops dead on some valid mssql queries

I created a script to perform a few select queries on a mssql database, but the script seems to stop dead on certain queries, without returning any errors. The browser receives a completely empty response from the server. On Firefox, it prompts to download a blank .php file; on Chrome it returns "Error 324 (net::ERR_EMPTY_RESPONSE): Unknown error."; and on IE8 it returns "Page cannot be displayed".

The query is

$sql = "SELECT *
    FROM EmployeeHours
    WHERE (EmployeeId = '".$staff_id."')
    AND (TimeIn > DATEADD(second, ".$week_start.", CONVERT(DATETIME, '1970-01-01', 102)))
    AND (TimeOut < DATEADD(second, ".$week_end.", CONVERT(DATETIME, '1970-01-01', 102))
        OR TimeOut IS NULL)
    ORDER BY UTCDateAdded DESC";
$query = mssql_query($sql);

$staff_id is a simple int. $week_start and $week_end are unix timestamps.

I don't think it's the query itself, because it runs fine in MSSQL Server Management Studio and returns the correct data. I can also simplify the query down to "SELECT TOP 1 * FROM EmployeeHours" with no WHERE conditions and it still fails.

Setting error_reporting to E_ALL has no effect. It doesn't execute anything past the mssql_query(). Anything printed or echoed before the query doesn't appear in the resulting file either.

I can verify that it is connected to the the correct database, because I can run some other queries on tables in the same database and get results through the script.

Tried switching to the ODBC extension instead of MSSQL ext and it would hang when using select queries with odbc_exec(). Found no fix for that either.

Finally got it to work by switching to PDO .

The problem is date type in result, use cast for date data types. Example: SELECT my_day, other_field FROM my_table use SELECT convert(varchar(19), my_day, 120) as my_day, other_field FROM my_table

use format your preference

In my case I was retrieving a float, a varchar(2) , two longs, and a "money" using PDO (dblib). The money value that was causing this was negative - a credit. I ended up changing the money field to a float, and the query would execute without the ERR_EMPTY_RESPONSE. Hopefully this will shorten someone else's time spent on this issue!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM