Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type binding not properly managed in PDO #41

Closed
david-garcia-garcia opened this issue Nov 7, 2014 · 3 comments
Closed

Type binding not properly managed in PDO #41

david-garcia-garcia opened this issue Nov 7, 2014 · 3 comments

Comments

@david-garcia-garcia
Copy link

This is something that's been carried along for a while, the PDO driver is incorrectly doing type binding under some situations:

  • SQL type varbinary needs to be mapped to PDO::PARAM_LOB manually because the driver won't.

When using BUFFERED QUERIES, type binding is mibehaving, all numeric types (int, smallint, etc.) are mapped to string when using fetchObject(), but even worse, these string are NULL Terminated String.

So for a "5" integer, you get a "5\0" string.

@yitam
Copy link
Contributor

yitam commented Feb 15, 2017

Hi @david-garcia-garcia , to fetch numeric values as numbers rather than strings simply do this $conn->setAttribute( PDO::SQLSRV_ATTR_FETCHES_NUMERIC_TYPE, true);

Please confirm.

@meet-bhagdev
Copy link
Contributor

@david-garcia-garcia : Does @yitam's recommendation help?

@yitam
Copy link
Contributor

yitam commented Mar 9, 2017

Hi @david-garcia-garcia

First and foremost, I believe the varbinary issue is already fixed by Pull Request #297. Please verify.

Moreover, for numeric types and buffered queries, I don't think the problem exists anymore. Here is my simple script:

$conn->exec("CREATE TABLE testTable (col1 int, col2 float, col3 real)"); 
$conn->setAttribute( PDO::SQLSRV_ATTR_FETCHES_NUMERIC_TYPE, true);

$stmt = $conn->prepare('INSERT INTO testTable VALUES (5, 0.57, 123.456)'); 
$stmt->execute(); 
        
$stmt = $conn->prepare("select * from testTable", array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL, PDO::SQLSRV_ATTR_CURSOR_SCROLL_TYPE => PDO::SQLSRV_CURSOR_BUFFERED));

if ($stmt->execute()){
     $row = $stmt->fetchObject();
     var_dump($row);
}

The output I got is

object(stdClass)#2 (3) {
  ["col1"]=>
  int(5)
  ["col2"]=>
  float(0.57)
  ["col3"]=>
  float(123.45600128174)
}

Without PDO::SQLSRV_ATTR_FETCHES_NUMERIC_TYPE set to true, I got output like this

object(stdClass)#2 (3) {
  ["col1"]=>
  string(1) "5"
  ["col2"]=>
  string(4) "0.57"
  ["col3"]=>
  string(7) "123.456"
}

Therefore, we will close this issue. Please feel free to reopen it if you still experience similar problems.

@yitam yitam closed this as completed Mar 9, 2017
@Hadis-Knj Hadis-Knj moved this from Backlog to Completed in msphpsql Mar 16, 2017
@Hadis-Knj Hadis-Knj removed this from Completed in msphpsql Mar 24, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants