PDO Insert抛出一个无法解释的"数组到字符串转换"错误

时间:2014-12-11 01:58:07

标签: php mysql arrays pdo

我有以下功能

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));
}

其中insertIdAfterInsert()是:

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: catch()是错误打印的地方

我正在传递

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

我的错误显示insertIdAfterInsert正在

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

,debugDumpParams如下所示:

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

(我不确定最后一个NULL,但是回显了一个正常运行的插入,并且出现了相同的NULL,所以我不太担心它)

任何人都可以告诉我为什么会抛出“数组到字符串转换”??

由于

1 个答案:

答案 0 :(得分:0)

感谢Phil!

错误是I-D-10-T,是由于不正确地处理PDO :: errorInfo()输出的数组。仍然不确定我的查询错误是什么,但我已经关闭了谷歌机器,并且更多回应找到我的答案。

感谢PHIL!

虽然草率很大,但我将trigger_error()更改为以下内容:

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

我的错误不是PDO