Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

does the PDO API only return string data types?

11 views
Skip to first unread message

TruthHunter

unread,
Jul 1, 2009, 1:15:49 PM7/1/09
to
Hi,

I couldn't find any other notes dealing with this, but if there are a
simple pointer would be appreciated.

Using PDO ODBC on Windows, connecting to SQL Server 2005, under PHP
5.2.10, it appears that the PHP data types of the columns fetched from
queries are always strings. This is different from the native MSSQL
API, for example, which does return integers (and interestingly, not
in all cases). Can anyone confirm this for me, or tell me what I'm
doing wrong? I discovered the problem when I was comparing columns I
thought were integer values, but were actually strings, which caused
havoc when the values were "99" and "100" instead of 99 and 100.

Here's some sample (psuedo) code that demonstrates the problem (on my
system, anyway):

<?php

$server = "server";
$user = "user";
$password = "password";
$db = "db";
$table = "table";
$bigintCol = "bigintCol";
$tinyintCol = "tinyintCol";
$stringCol = "bigintCol";
$sql = "SELECT TOP 1 * FROM " . $table . " WHERE " . $bigintCol . " IS
NOT NULL AND " . $tinyintCol . " IS NOT NULL AND " . $stringCol . " IS
NOT NULL";

$conn = mssql_connect($server, $user, $password);
mssql_select_db($db, $conn);
$result = mssql_query($sql, $conn);
$row = mssql_fetch_assoc($result);
print("Native:\n");
print(gettype($row[$bigintCol]) . "\n");
print(gettype($row[$tinyintCol]) . "\n");
print(gettype($row[$stringCol]) . "\n");
mssql_close($conn);

$conn = new PDO("odbc:Driver={SQL Server};Server=" . $server .
";Database=" . $db . ";Uid=" . $user . ";Pwd=" . $password . ";");
$stmt = $conn->query($sql);
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$resultArray = $stmt->fetchAll();
$row = $resultArray[0];
print("PDO:\n");
print(gettype($row[$bigintCol]) . "\n");
print(gettype($row[$tinyintCol]) . "\n");
print(gettype($row[$stringCol]) . "\n");

?>

Output:
-----------
Native:
string
integer
string
PDO:
string
string
string

TruthHunter

unread,
Jul 1, 2009, 2:07:40 PM7/1/09
to
Sorry - a cut and paste problem:

$stringCol = "bigintCol";

should be:

$stringCol = "stringCol";

Behavior is still as described...

0 new messages