Samutz
Samutz

Reputation: 2300

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.

Upvotes: 1

Views: 1377

Answers (3)

Jesse Q
Jesse Q

Reputation: 1671

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!

Upvotes: 0

Germ&#225;n Ojeda
Germ&#225;n Ojeda

Reputation: 1

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

Upvotes: 0

Samutz
Samutz

Reputation: 2300

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.

Upvotes: 1

Related Questions