简体   繁体   中英

PDO Insert throws an unexplained “Array to string conversion” error

I've got the following function

public function addProduct($type, $title, $url
        , $small_image_id, $large_image_id, $number, $number_type
        , $formatted_number, $fk) {

    switch (strtoupper($type)) {
        case 'FOO':
            $ind = 1;
            break;
        case 'BARR':
            $ind = 2;
            break;
        default:
            return null;
    }

    $sql=   "INSERT INTO product(fk_id, type_id, title, url, ".
                                        "sm_image_id, lg_image_id, number, ".
                                        "num_type, num_formatted)".
            "values(?, ?, ?, ?, ?, ?, ?, ?, ?)";

    return $this->insertIdAfterInsert($sql,'iissiiiss',array($fk
            , $ind, $title , $url, $small_image_id
            , $large_image_id, $number, $number_type, $formatted_number));
}

Where insertIdAfterInsert() is:

private function insertIdAfterInsert($sql,$types = null,$params = null) {
    $stmt = $this->conn->prepare($sql);
    if ($stmt === false) {
        trigger_error('Error: ' . $this->conn->errno . ' ' . $this->conn->error, E_USER_ERROR);
    }

    if($types&&$params)
    {
        $typeArr = str_split($types);
        for($i=0; $i< count($params); $i++) {
            if ($typeArr[$i] === 'i') {
                $bind_type = PDO::PARAM_INT;
            } elseif($typeArr[$i]==='s'){
                $bind_type = PDO::PARAM_STR;
            }
            $stmt->bindValue($i+1, $params[$i], $bind_type);
        }
    }

echo (var_dump($stmt->debugDumpParams()));

    try {
        if (!$stmt->execute()) {
            trigger_error('Error: ' . $this->conn->errorCode() . ' ' . $this->conn->errorInfo(), E_USER_ERROR);
        }
    } catch (Exception $e) {
        trigger_error('Error: ' . $this->conn->errorCode() . ' ' . $this->conn->errorInfo(), E_USER_ERROR);
    }
    $stmt = null;
    return $this->conn->lastInsertId();
}

UPDATE: The catch() is where the error prints

I'm passing in

addProduct('Foo', 'Cohort', 'http://www.gooo...', 0, 0, 0, ' ', ' ', 210)

My error shows that insertIdAfterInsert is getting

insertIdAfterInsert('INSERT INTO aff...', 'iissiiiss', Array)

and the debugDumpParams looks like this:

SQL: [183] INSERT INTO affiliate_product(fk_id, type_id, title, url, sm_image_id, lg_image_id, number, num_type, num_formatted)values(?, ?, ?, ?, ?, ?, ?, ?, ?)
Params:  9
Key: Position #0:
paramno=0
name=[0] ""
is_param=1
param_type=1
Key: Position #1:
paramno=1
name=[0] ""
is_param=1
param_type=1
Key: Position #2:
paramno=2
name=[0] ""
is_param=1
param_type=2
Key: Position #3:
paramno=3
name=[0] ""
is_param=1
param_type=2
Key: Position #4:
paramno=4
name=[0] ""
is_param=1
param_type=1
Key: Position #5:
paramno=5
name=[0] ""
is_param=1
param_type=1
Key: Position #6:
paramno=6
name=[0] ""
is_param=1
param_type=1
Key: Position #7:
paramno=7
name=[0] ""
is_param=1
param_type=2
Key: Position #8:
paramno=8
name=[0] ""
is_param=1
param_type=2
NULL

(i'm not sure about that last NULL, but echoed a functioning insert and the same NULL appears so i'm not too worried about it)

Can anyone tell me why this would be throwing the "Array to string conversion"??

Thanks

Thanks to Phil!

The error was ID-10-T and was due to improperly handling the array that PDO::errorInfo() outputs. Still not sure what the error is with my query but I'm off to the Google machine and more echoes to find my answer.

THANKS PHIL!

All though sloppy I altered my trigger_error() to the following:

trigger_error('Error: ' . $this->conn->errorCode() . ' ' . var_dump($this->conn->errorInfo()), E_USER_ERROR);

My error was not with PDO

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