Re: DB_FETCHMODE_ASSOC result differs for MySQL and MS-SQL
| From: | Markus Wolff | Date: | Sat, 18 Jan 2003 15:46:14 +0000 |
| Subject: | Re: DB_FETCHMODE_ASSOC result differs for MySQL and MS-SQL | ||
| References: | 1 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-3338@lists.php.net to get a copy of this message | ||
On Sat, 18 Jan 2003 06:17:24 -0400
"Gunther" <g@guntherrichter.com> wrote:
> I have a problem with PHP 4.3.0, latest PEAR running under w2k with either
> MySQL or MS-SQL. With MySQL everything is fine, running the same application
> just
> changing the DB server to MS-SQL I get different results.
>
> I am using fetchRow(DB_FETCHMODE_ASSOC) to get my DB rows.
> Under MySQL (correct) a row would look like this:
> 'fieldname' => value (e.g. [whateverfield] => 9 )
>
> Under MS-SQL I get:
> 0 => value 'fieldname' => value (e.g. [0] => 9 [whateverfield] => 9 )
>
> It seems that under MS-SQL DB_FETCHMODE_ASSOC AND DB_FETCHMODE_ORDERED
> are both on at the same time.
Hi Gunther,
PEAR::DB´s MSSQL driver uses mysql_fetch_array() when you´re using
DB_FETCHMODE_ASSOC - it is normal for this function to behave this way.
The driver does this because in PHP versions previous to PHP 4.3.0 it
was not possible to compile the MSSQL extension under Linux. Instead,
you had to use the Sybase extension, which is sort of compatible to
MSSQL, but does not provide the complete set of functions.
A possible solution is writing you own driver class. Make a new file
that is called "myMSSQL.php" (or something you like better). Now write
this piece of code into the new file:
// Begin code
require_once('DB/mssql.php');
class DB_myMSSQL extends DB_mssql
{
function fetchInto($result, &$ar, $fetchmode, $rownum=null)
{
if ($rownum !== null) {
if (!@mssql_data_seek($result, $rownum)) {
return null;
}
}
if ($fetchmode & DB_FETCHMODE_ASSOC) {
$ar = @mssql_fetch_assoc($result);
} else {
$ar = @mssql_fetch_row($result);
}
if (!$ar) {
/* This throws informative error messages,
don't use it for now
if ($msg = mssql_get_last_message()) {
return $this->raiseError($msg);
}
*/
return null;
}
return DB_OK;
}
}
// End code
Now you have created a new driver class that uses the function
mssql_fetch_assoc() instead of mssql_fetch_array(). The new driver
should behave as intended, but will not work when used on Linux systems
with a PHP version that is using the --with-sybase option instead of
--with-mssql (all versions below 4.3.0). On Windows systems, it should
always work.
You can use your new driver by simply modifying your DSN like this:
$db = DB::connect('myMSSQL://user:pwd@dbserver/dbname');
Regards,
Markus
--
Markus Wolff <wolff@21st.de>