我遇到了一件非常奇怪的事情。
这会引发500服务器错误:
function writeMsg() {
$id='0000000625';
$translation = 'word';
try {
$stmt = $conn->prepare("UPDATE table SET HUN=? WHERE ID=?");
$stmt->execute(array($translation,$id));
$response["success"] = 1;
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
$response["success"] = 0;
}
echo 'RESPONSE: '.$response["success"];
}
writeMsg();
运行良好:
$id='0000000625';
$translation = 'word';
try {
$stmt = $conn->prepare("UPDATE table SET HUN=? WHERE ID=?");
$stmt->execute(array($translation,$id));
$response["success"] = 1;
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
$response["success"] = 0;
}
echo 'RESPONSE: '.$response["success"];
我只删除了第一行和最后两行。 有人对此有解释吗?
答案 0 :(得分:0)
您可能错过了$conn
变量范围
function writeMsg() {
global $conn;
$id = '0000000625';
$translation = 'word';
try {
$stmt = $conn->prepare("UPDATE table SET HUN=? WHERE ID=?");
$stmt->execute(array($translation, $id));
$response["success"] = 1;
} catch(\PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
$response["success"] = 0;
}
echo 'RESPONSE: ' . $response["success"];
}
writeMsg();
只需使用global $conn
从全局范围中检索$conn
变量即可。点击http://php.net/manual/en/language.variables.scope.php查看全球关键字