pdo - SQL query in php function 500 server error -
this question has answer here:
- giving function access outside variable 5 answers
i ran strange thing.
this throws 500 server error:
function writemsg() { $id='0000000625'; $translation = 'word'; try { $stmt = $conn->prepare("update table set hun=? id=?"); $stmt->execute(array($translation,$id)); $response["success"] = 1; } catch(pdoexception $e) { echo 'error: ' . $e->getmessage(); $response["success"] = 0; } echo 'response: '.$response["success"]; } writemsg();
this runs fine:
$id='0000000625'; $translation = 'word'; try { $stmt = $conn->prepare("update table set hun=? id=?"); $stmt->execute(array($translation,$id)); $response["success"] = 1; } catch(pdoexception $e) { echo 'error: ' . $e->getmessage(); $response["success"] = 0; } echo 'response: '.$response["success"];
i removed first , last 2 lines. has explanation this?
you've missed $conn
variable scope
function writemsg() { global $conn; $id = '0000000625'; $translation = 'word'; try { $stmt = $conn->prepare("update table set hun=? id=?"); $stmt->execute(array($translation, $id)); $response["success"] = 1; } catch(\pdoexception $e) { echo 'error: ' . $e->getmessage(); $response["success"] = 0; } echo 'response: ' . $response["success"]; } writemsg();
just use global $conn
retrieve $conn
variable global scope. check more @ http://php.net/manual/en/language.variables.scope.php global keyword
Comments
Post a Comment