Package home | Report new bug | New search | Development Roadmap Status: Open | Feedback | All | Closed Since Version 1.12.2

Bug #2907 DB_FETCHMODE_ASSOC not working with mssql
Submitted: 2004-12-06 11:27 UTC
From: Phil dot H at gmx dot net Assigned: danielc
Status: Bogus Package: DB
PHP Version: 5.0.2 OS: Linux
Roadmaps: (Not assigned)    
Subscription  


 [2004-12-06 11:27 UTC] Phil dot H at gmx dot net
Description: ------------ DB_FETCHMODE_ASSOC works on my Windows XAMPP System but, on linux I do not get any result. DB_FETCHMODE_ORDERERD works on both systems. I have just updated pear and php to 5.02. Both systems should have the same versions... The problem seems to be mssql related, on linux I use freetds and on windows I use the sql client libaries... Reproduce code: --------------- $dsn = "mssql://$user:$pass@$host/$db_name"; $db = DB::connect($dsn); $sql="SELECT "; $sql.="[id], [name] "; $sql.="FROM [ticket].[dbo].[tbl_ticket] WHERE id=1"; if (DB::isError($result)) { die ( $result->getMessage() ); } while($row = $result->fetchRow(DB_FETCHMODE_ASSOC)) { print_r($row); } Expected result: ---------------- That should be the result: Array ( [id] => 1 [name] => Philipp ) on the linux system I get no result. Actual result: -------------- on linux NOTHING on windows as expected

Comments

 [2004-12-06 16:59 UTC] danielc
Your example is incomplete. You don't call query(). More importantly, a test must be 100% complete and self contained. It needs to connect, create a SIMPLE table, insert a row of data, select it, then you can try fetching it and finally the test must drop the table. This way anyone on any machine can exactly reproduce your situation.
 [2004-12-07 07:01 UTC] Phil dot H at gmx dot net
yes you are right: the query is between the sql und the if. $sql =... $result = $db->query($sql); if (DB::...
 [2004-12-07 10:32 UTC] Phil dot H at gmx dot net
so this is my testsuite, I that it is 100% complete. <?php # PEAR MSSQL FETCHROW Testsuite, created 7.12.2004 by Phil dot H at gmx dot net require_once 'DB.php'; $user = 'SA'; $pass = 'secret'; $host = 'dbhost'; $db_name = 'dbtest'; # Data Source $dsn = "mssql://$user:$pass@$host/$db_name"; $conn = DB::connect($dsn); # Create Table $result = $conn->query ("CREATE TABLE animal (name CHAR(40), category CHAR(40))"); if (DB::isError ($result)) die ("CREATE TABLE failed: " . $result->getMessage () . "\n"); $result = $conn->query ("INSERT INTO animal (name,category) VALUES ('snake','reptile');"); if (DB::isError ($result)) die ("INSERT failed: " . $result->getMessage () . "\n"); # Select ORDERED $result = $conn->query ("SELECT name, category FROM animal"); if (DB::isError ($result)) die ("SELECT failed: " . $result->getMessage () . "\n"); printf ("ORDERED Result set contains %d rows and %d columns\n", $result->numRows (), $result->numCols ()); while ($row = $result->fetchRow (DB_FETCHMODE_ORDERED)) printf ("%s, %s\n", $row[0], $row[1]); $result->free (); printf ("<br>"); # Select ASSOC $result = $conn->query ("SELECT name, category FROM animal"); if (DB::isError ($result)) die ("SELECT failed: " . $result->getMessage () . "\n"); printf ("ASSOC Result set contains %d rows and %d columns\n", $result->numRows (), $result->numCols ()); while ($row = $result->fetchRow (DB_FETCHMODE_ASSOC)) printf ("%s, %s\n", $row["name"], $row["category"]); $result->free (); # Remove Table $result = $conn->query ("DROP TABLE animal"); if (DB::isError ($result)) die ("DROP TABLE failed: " . $result->getMessage () . "\n"); $conn->disconnect(); # EVERYTHING OK: # XAMPP Windows Version 1.4.6 # Apache/2.0.50 (Win32) mod_ssl/2.0.50 OpenSSL/0.9.7c PHP/5.0.1 Server at localhost Port 80 # MSSQL Client Tools installed #result: /* ORDERED Result set contains 1 rows and 2 columns snake , reptile ASSOC Result set contains 1 rows and 2 columns snake , reptile */ # NOT (fully) WORKING: # Debian Linux: # PHP Version 5.0.2-1.dotdeb.5 # Apache 1.3.3 # Freetds # result: /* ORDERED Result set contains 1 rows and 2 columns snake , reptile ASSOC Result set contains 1 rows and 2 columns */ ?>
 [2004-12-07 17:32 UTC] danielc
Good test. Thanks. Since I don't have access to a linux server, everyone needs your assistance to figure out what's going on here. The only idea I have is some DBMS's have issues when running the same query two times. Try adding a $result->free() in between the two attempts and see what happens. Other than that, you're going to need to do some serious debugging. When you isolate the issue, come up with a patch. I'll be glad to apply it.
 [2004-12-07 18:19 UTC] Phil dot H at gmx dot net
that makes no sense, I have created only one query at the first time and the problem was the same. Only for this test I have included both...
 [2004-12-07 19:20 UTC] danielc
I've worked with several other DBMS's. It's a legitimate question. I hope you will add the free() call to the test just to make sure. If that isn't it, again, I hope you'll take the time to debug what's going on so everyone can benefit.
 [2004-12-08 07:06 UTC] Phil dot H at gmx dot net
if you look at the code again, you will see that there is already a result->free() between the two queries. above "printf ("<br>"); " you will find it. Or do I not understand you fully? Am I the only one who connects to mssql server on a linux system? The normal mssql_fetch_row without pear makes no problem.
 [2004-12-08 16:25 UTC] danielc
Glad there's a free() in there. In open source development, users help make programs better. (That's what I'm doing.) I hope you'll do the same by tracking down what's going on. There's no other way this is going to get fixed. Thanks.
 [2004-12-09 14:38 UTC] Phil dot H at gmx dot net
Since I have changed line 198 in mssql.php, everything seems to work: if ($fetchmode & DB_FETCHMODE_ASSOC) { # Bugfix by Philipp H | line 198 #$arr = @mssql_fetch_array($result, MSSQL_ASSOC); $arr = @mssql_fetch_array($result); if ($this->options['portability'] & DB_PORTABILITY_LOWERCASE && $arr) { $arr = array_change_key_case($arr, CASE_LOWER); } } else { $arr = @mssql_fetch_row($result); }
 [2004-12-09 17:17 UTC] danielc
Removing MSSQL_ASSOC from the mssql_fetch_array() call isn't a real solution because that causes the function to produce both enumerated and associative arrays. What happens if you run the following PHP-only script? <?php $con = mssql_connect('localhost', 'ptuser', 'pts8ft'); $res = mssql_query("CREATE TABLE animal (name CHAR(10))", $con); $res = mssql_query("INSERT INTO animal (name) VALUES ('snake')", $con); $res = mssql_query("SELECT name FROM animal", $con); $arr = mssql_fetch_array($res, MSSQL_ASSOC); print_r($arr); ?>
 [2004-12-09 17:20 UTC] danielc
Oops, I forgot to throw on the drop table statement after the print_r. Please append the following line to the example script. $res = mssql_query("DROP TABLE animal", $con);
 [2004-12-10 07:40 UTC] Phil dot H at gmx dot net
You also forgot, to select a database... So this is my test script: <?php $con = mssql_connect('dbhost', 'dbuser', 'dbpass'); mssql_select_db('database', $con); $res = mssql_query("CREATE TABLE animal (name CHAR(10))", $con); $res = mssql_query("INSERT INTO animal (name) VALUES ('snake')", $con); $res = mssql_query("SELECT name FROM animal", $con); $arr = mssql_fetch_array($res, MSSQL_ASSOC); $res = mssql_query("DROP TABLE animal", $con); print_r($arr); /* result: Warning: Wrong parameter count for mssql_fetch_array() in /var/www/oop/d1.php on line 7 */ ??
 [2004-12-10 15:57 UTC] danielc
This sounds like a FreeTDS issue.
 [2004-12-11 00:28 UTC] Phil dot H at gmx dot net
yes but perhaps, you can detect which db libary is used and handle this bug in you class...
 [2004-12-11 17:48 UTC] danielc
The best thing to do is have a discussion with the FreeTDS people and see if it's their problem and if they'll fix it in FreeTDS. Modifying DB for this is inappropriate.