0

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

5th Jan 2017, 9:41 PM
Aziz F Dagli
Aziz F Dagli - avatar
3 ответов
0
where is your pdo object established
5th Jan 2017, 9:49 PM
Louis Milotte
Louis Milotte - avatar
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.
6th Jan 2017, 4:53 AM
Aziz F Dagli
Aziz F Dagli - avatar
- 1
where do you include your db connection in your query file
6th Jan 2017, 1:50 PM
Louis Milotte
Louis Milotte - avatar