PEAR is archived and read-only

This mirror preserves historical PEAR package releases and metadata so existing references remain available.

Home » Database » DB » Bug #2907

DB_FETCHMODE_ASSOC not working with mssql

Details

Submitted2004-12-06 11:27 UTC
FromPhil dot H at gmx dot net
Assigneddanielc
StatusBogus
PackageDB
PHP Version5.0.2
OSLinux
Roadmaps(Not assigned)

Comments

[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

[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 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-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-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-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-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...