pdo - SQL query in php function 500 server error -


this question has answer here:

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

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -