使用GAE云sql进行PHP mySQL更新

时间:2014-05-19 23:59:37

标签: php mysql sql google-app-engine cloud

Persons表有一个名为“data”的字段和另一个名为“id”的字段,它自动递增。 它有1个条目,我正在尝试更新该条目。 我正在使用GAE Cloud Sql for php。 我成功连接到数据库,然后执行以下操作:

$data="Please do not go.";

try {
                   $stmt = $db->prepare('INSERT INTO Persons (data) VALUES (:theData)');
                   $stmt->execute(array(':theData' => htmlspecialchars($data)));
                   $affected_rows = $stmt->rowCount();
                   // Log $affected_rows.
} catch (PDOException $ex) {
                    // Log error.
                    die(json_encode(
                    array('outcome' => false, 'message' => 'PIE.')
                    )
                    );
}

$data="Please go away.";

try {
          $stmt = $db->prepare('UPDATE Persons (data) VALUES (:theData) WHERE id=1');
          $stmt->execute(array(':theData' => htmlspecialchars($data)));
} catch (PDOException $ex) {
          // Log error.
          die(json_encode(
                    array('outcome' => false, 'message' => 'LOL YOU FAILED')
                    )
                    );
}

foreach($db->query('SELECT * from Persons') as $row) {

                    echo $row['data'];

}

$db = null;

输出结果为:

请不要去。

而不是:

请离开。

我用来更新表的代码有什么问题?

1 个答案:

答案 0 :(得分:0)

UPDATE documented here与[{1}}关键字不是INSERT形成的。{/ p>

VALUES

您希望使用UPDATE [LOW_PRIORITY] [IGNORE] table_reference SET col_name1={expr1|DEFAULT} [, col_name2={expr2|DEFAULT}] ... [WHERE where_condition] [ORDER BY ...] [LIMIT row_count]

指定值/列关系
SET