|
| 1 | +--TEST-- |
| 2 | +Test fetch from binary, varbinary, varbinary(max), image columns, without setting binary encoding. |
| 3 | +--DESCRIPTION-- |
| 4 | +Verifies GitHub issue 270 is fixed, users could not retrieve the data as inserted in binary columns without setting the binary encoding either on stmt or using bindCoulmn encoding. |
| 5 | +This test verifies that the data inserted in binary columns can be retrieved using fetch, fetchColumn, fetchObject, and fetchAll functions. |
| 6 | + |
| 7 | +--FILE-- |
| 8 | +<?php |
| 9 | + |
| 10 | +require_once("autonomous_setup.php"); |
| 11 | + |
| 12 | +$tableName = 'test_binary'.rand(); |
| 13 | +$columns = array( 'col1', 'col2', 'col3', 'col4'); |
| 14 | + |
| 15 | +// Connect |
| 16 | +$conn = new PDO( "sqlsrv:server=$serverName;Database=tempdb", $username, $password ); |
| 17 | + |
| 18 | +$sql = "CREATE TABLE $tableName ( $columns[0] binary(50), $columns[1] VARBINARY(50), $columns[2] VARBINARY(MAX), $columns[3] image)"; |
| 19 | +$conn->exec($sql); |
| 20 | + |
| 21 | +$icon = base64_decode("This is some text to test retrieving from binary type columns"); |
| 22 | + |
| 23 | +// Insert data using bind parameters |
| 24 | +$sql = "INSERT INTO $tableName($columns[0], $columns[1], $columns[2], $columns[3]) VALUES(?, ?, ?, ?)"; |
| 25 | +$stmt = $conn->prepare($sql); |
| 26 | +$stmt->bindParam(1, $icon, PDO::PARAM_LOB, null, PDO::SQLSRV_ENCODING_BINARY); |
| 27 | +$stmt->bindParam(2, $icon, PDO::PARAM_LOB, null, PDO::SQLSRV_ENCODING_BINARY); |
| 28 | +$stmt->bindParam(3, $icon, PDO::PARAM_LOB, null, PDO::SQLSRV_ENCODING_BINARY); |
| 29 | +$stmt->bindParam(4, $icon, PDO::PARAM_LOB, null, PDO::SQLSRV_ENCODING_BINARY); |
| 30 | +$stmt->execute(); |
| 31 | + |
| 32 | +// loop through each column in the table |
| 33 | +foreach ($columns as $col){ |
| 34 | + test_fetch($conn, $tableName, $col, $icon); |
| 35 | +} |
| 36 | +// DROP table |
| 37 | +$conn->query("DROP TABLE $tableName") ?: die(); |
| 38 | + |
| 39 | +//free statement and connection |
| 40 | +$stmt = null; |
| 41 | +$conn = null; |
| 42 | + |
| 43 | +print_r("Test finished successfully"); |
| 44 | + |
| 45 | +//calls various fetch methods |
| 46 | +function test_fetch($conn, $tableName, $columnName, $input){ |
| 47 | + |
| 48 | + $len = strlen($input); |
| 49 | + $result = ""; |
| 50 | + $sql = "SELECT $columnName from $tableName"; |
| 51 | + |
| 52 | + $stmt = $conn->query($sql); |
| 53 | + $stmt->bindColumn(1, $result, PDO::PARAM_LOB); |
| 54 | + $stmt->fetch(PDO::FETCH_BOUND); |
| 55 | + //binary is fixed size, to evaluate output, compare it using strncmp |
| 56 | + if( strncmp($result, $input, $len) !== 0){ |
| 57 | + print_r("\nRetrieving using bindColumn failed"); |
| 58 | + } |
| 59 | + |
| 60 | + $result = ""; |
| 61 | + $stmt = $conn->query($sql); |
| 62 | + $stmt->bindColumn(1, $result, PDO::PARAM_LOB, 0 , PDO::SQLSRV_ENCODING_BINARY); |
| 63 | + $stmt->fetch(PDO::FETCH_BOUND); |
| 64 | + if( strncmp($result, $input, $len) !== 0){ |
| 65 | + print_r("\nRetrieving using bindColumn with encoding set failed"); |
| 66 | + } |
| 67 | + |
| 68 | + $result = ""; |
| 69 | + $stmt = $conn->query($sql); |
| 70 | + $result = $stmt->fetchColumn(); |
| 71 | + if( strncmp($result, $input, $len) !== 0){ |
| 72 | + print_r("\nRetrieving using fetchColumn failed"); |
| 73 | + } |
| 74 | + |
| 75 | + $result = ""; |
| 76 | + $stmt = $conn->query($sql); |
| 77 | + $result = $stmt->fetchObject(); |
| 78 | + if( strncmp($result->$columnName, $input, $len) !== 0){ |
| 79 | + print_r("\nRetrieving using fetchObject failed"); |
| 80 | + } |
| 81 | + |
| 82 | + $result = ""; |
| 83 | + $stmt = $conn->query($sql); |
| 84 | + $result = $stmt->fetchAll( PDO::FETCH_COLUMN ); |
| 85 | + if( strncmp($result[0], $input, $len) !== 0){ |
| 86 | + print_r("\nRetrieving using fetchAll failed"); |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +?> |
| 91 | +--EXPECT-- |
| 92 | +Test finished successfully |
0 commit comments