Jump to content

Archived

This topic is now archived and is closed to further replies.

CamW

PHP returning incorrect results from a query

Recommended Posts

Hi all,

I'm having a peculiar problem where PHP is occasionally returning incorrect results on a query.  I'm connecting to the db like so:

[code]$conn =& ADONewConnection('odbc_mssql');
$dsn = "Driver={SQL Server};Server=$server;Database=$db;";

$conn->Connect($dsn, $dBUser, $dBPass);[/code]

This works fine.  Then, I execute a sql statement like so:

[code]$result = $conn->Execute($query);[/code]

Again, this works fine, and these code snippets exist in a function that I've been using for months now, and I've had no problems with any other scripts accessing the database.  However, today I noticed some database results that looks suspicious, and upon further inspection, the query was indeed returing returning results inconsistent with the data in the database.  To debug, I threw in some echo commands to see what was going on:

[code]echo $query . "<br />";
$result = $conn->Execute($query);
echo "<pre>";
print_r($result->fields);
echo "</pre>";[/code]

One of the queries that was returning incorrect data was this:

[code]SELECT COUNT(id) AS theCount FROM ccwilki2.tblAnswer WHERE answer = 'Natalie Poole' AND survey_id = 17 AND question_id = 31[/code]

Upon executing this query, the print_r of the $result->fields array revealed that $conn->Execute had returned an incorrect number (0).  The really weird thing is, if I cut and paste this query directly into the SQL Server Management Studio and execute it, it gives the correct result (6)!  That is without changing the query a bit.  To me, it looks like the problem is coming from the $conn->Execute() function.  Is this a bug?  Have any of you encountered this problem before?

The problem only occurs with a small minority of the queries, but the problem queries are always the same.  I am certain that the connection created in PHP points to the same database as SQL Server Management Studio was accessing when I checked the results.  The script username only has access to one mssql database, and I signed on to management studio with that username.

Any help would be most appreciated!  This is making me go prematurely gray!  Thanks in advance.

-Cameron

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.