Jump to content


PHP returning incorrect results from a query

  • Please log in to reply
No replies to this topic

#1 CamW

  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 07 July 2006 - 03:32 PM

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:

$conn =& ADONewConnection('odbc_mssql');
$dsn = "Driver={SQL Server};Server=$server;Database=$db;";
$conn->Connect($dsn, $dBUser, $dBPass);

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

$result = $conn->Execute($query);

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:

echo $query . "<br />";
$result = $conn->Execute($query);
echo "<pre>";
echo "</pre>";

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

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

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.


0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users