New course! Every coder should learn Generative AI!
Try a free lesson0
PDO Exception don't catch
try{ line53 -> $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); $ekle = $db->prepare("INSERT INTO data SET name=?, email=?"); $insert = $ekle->execute(array("$name","$e_mail")); if($insert) { header ("Location:sucess.html"); } else{ throw ("Error"); } } catch(PDOException $e){ echo $e->getMessage(); header ("Location:error.html"); } this code don't catch exception. SQL isn't work. Take error Notice: Undefined variable: db in C:\xampp\htdocs\.....\.....php on line 53 Fatal error: Call to a member function prepare() on null in C:\xampp\htdocs\.....\....php on line 53
3 Answers
0
where is your pdo object established
0
<?php
$host = 'mysql:host=localhost;dbname=fro';
$user = 'root';
$pw = '';
try{
$db = new PDO($host,$user,$pw);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e){
echo 'Bağlantı hatası'.$e->getMessage();
}
$db->query("SET NAMES 'utf8'");
?>
I established different file.
- 1
where do you include your db connection in your query file