简体   繁体   中英

PDO Error when ODBC_Exec works correctly

I have a project I am converting and am having several weird results with one script so far. The conversion is from procedural ODBC to PDO. I have the following differing result sets when I perform the old and new query constructs.

$con = odbc_connect("database","user,"pass");
if(!$con){
    exit("error");
}
    $sql = "SELECT column,column2 FROM table WHERE column3=number AND ((column2 Like '%hold%') OR (column2 Like '%panic%'))";
$result = odbc_exec($con,$sql); 
if(!$result)
    exit("error");
$temp = array();
    while($row = odbc_fetch_array($result)){
        if(($preferedKey !="")&&isset($row[$preferedKey])){
            $temp[$row[$preferedKey]] = $row;
        }
        else {
            array_push($temp, $row);
        }
    }
odbc_close($con);
return $temp;

this returns a multi-dimensional array

Array(
[0] => Array
    (
        [column] => 026
        [column2] => COLLECTION HOLD UP 
    )

[1] => Array
    (
        [column] => 027
        [column2] => MORTGAGE HOLD UP
    )

[2] => Array
    (
        [column] => 028
        [column2] => HOLD UP 
    )
)

When I use PDO to get the information with all other variables/parameters kept the same I get the following (code included as well):

try{
    $data = PDO_Connect($database)->query($sql)->fetchAll(PDO::FETCH_ASSOC);

    return ((count($data)==1&&$assoc==false) ? $data[0] : ($preferedKey!="" ? (isset($data[0][$preferedKey]) ? changeKey($data,$preferedKey) : $data) : $data));
} catch(PDOException $e){
    die($e);
    //error_redirect($e);   
}

Array(
[0] => Array
    (
        [column] => 026
        [column2] => 
    )

[1] => Array
    (
        [column] => 027
        [column2] => 
    )

[2] => Array
    (
        [column] => 028
        [column2] => 
    )
)

If I remove column and just select column2, first method returns:

Array(
[0] => Array
    (
        [column2] => COLLECTION HOLD UP 
    )

[1] => Array
    (
        [column2] => MORTGAGE HOLD UP
    )

[2] => Array
    (
        [column2] => HOLD UP 
    )
)

PDO Method Errors with:

PDOException: SQLSTATE[SL009]: <>: 0 [unixODBC][Driver Manager]No columns were bound prior to calling SQLFetch or SQLFetchScroll (SQLFetchScroll[0] at /build/php7.0-x25jsn/php7.0-7.0.13/ext/pdo_odbc/odbc_stmt.c:559)

I've successfully converted most of the rest of the site, and cannot find any cause/reason this should be happening.


EDIT @ 13:42 Central

After searching, I tried to bindColumn as LOB to see what happened:

$sql = "SELECT column,column2 FROM table WHERE column3=number AND ((column2 Like '%hold%') OR (column2 Like '%panic%'))";
$stmt = PDO_Connect($database)->prepare($sql);
$stmt->bindColumn("Description",$desc,PDO::PARAM_LOB)
$stmt->execute();
while($data = $stmt->fetch(PDO::FETCH_BOUND)){
    fpassthru($desc);
    darray(get_defined_vars());
}

I get fpassthru() expects parameter 1 to be resource, null given

removing column from $sql gives me the same error of no bound columns.

I also tried CAST(column2 as varchar) and it gets a value of 1 Casting to INT throws an error as expected, but has the data I need from the DB in the error

Error -157: Cannot convert 'COLLECTION HOLD UP ' to a integer



EDIT @ 16:40 Central

Few more hours searching and found a couple php bug reports about long varchars not working, just returning null values. One of them has been open for almost 10 years. I'm not sure this problem is solvable in a strict PDO environment.

I'll have to try to get info with PDO, and if a null result is returned, try with ODBC_Exec, and then only if both are null, it is a truly null column or no records exist.

Does that seem right?

https://github.com/php/php-src/blob/php-7.3.9/ext/pdo_odbc/odbc_stmt.c#L622

if (colsize < 256 && !S->going_long) {
    // ......
} else {
    // ......
}

->

if (1) {
    // ......
} else {
    // ......
}

re-compile PDO_ODBC and it works magically

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM