解析错误:语法错误,意外'=>' (T_DOUBLE_ARROW)

时间:2014-06-17 08:24:23

标签: php

为什么我会

  

解析错误:语法错误,意外' =>' (T_DOUBLE_ARROW)

当我在准备好的PDO声明中使用它时。

            $stmt->execute(
            ':name' => $nme,
            ':street' => $strt,
            ':suburb' => $sbrb,
            ':city' => $cty,
            ':province' => $cnty,
            ':country' => $cntry
        );

这不是

的例子
$id = 5;
$name = "Joe the Plumber";

try {
  $pdo = new PDO('mysql:host=localhost;dbname=someDatabase', $username, $password);
  $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

  $stmt = $pdo->prepare('UPDATE someTable SET name = :name WHERE id = :id');
  $stmt->execute(array(
    ':id'   => $id,
    ':name' => $name
  ));

  echo $stmt->rowCount(); // 1
} catch(PDOException $e) {
  echo 'Error: ' . $e->getMessage();
}

3 个答案:

答案 0 :(得分:2)

请准确一点代码:

$stmt->execute( **array(**
            ':name' => $nme,
            ':street' => $strt,
            ':suburb' => $sbrb,
            ':city' => $cty,
            ':province' => $cnty,
            ':country' => $cntry
        **)**);

答案 1 :(得分:2)

将其更改为(注意数组):

$stmt->execute(array(
        ':name' => $nme,
        ':street' => $strt,
        ':suburb' => $sbrb,
        ':city' => $cty,
        ':province' => $cnty,
        ':country' => $cntry
));

答案 2 :(得分:1)

我认为您应该传递数组,因此请将代码更改为:

    $stmt->execute(array(
        ':name' => $nme,
        ':street' => $strt,
        ':suburb' => $sbrb,
        ':city' => $cty,
        ':province' => $cnty,
        ':country' => $cntry
    ));