kalster Posted December 21, 2014 Share Posted December 21, 2014 in php, pdo, the code does not output any error messages when table is not found. try { $stmt = $dbh->prepare("SELECT * FROM 1234"); $stmt->execute(); $row = $stmt->fetch(); } catch (PDOException $e) { echo $e->getMessage().' in '.$e->getFile().' on line '.$e->getLine(); } the code only seems to work when the following code is placed just under the "try {" $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); the above code is used just below the connection to the database code. how to get the try - catch code to work without using the setAttribute code every time for prepared statements? Quote Link to comment https://forums.phpfreaks.com/topic/293218-pdo-setattribute-and-prepared-statements/ Share on other sites More sharing options...
mac_gyver Posted December 21, 2014 Share Posted December 21, 2014 the above code is used just below the connection to the database code. you would need to post that code as a starting point for us to have a chance at helping you with the problem. any chance you have code that opens a new/second database connection after your first connection, even if it is using the same php variable to hold the instance of the PDO class, but doesn't have the setAttribute() statements? any chance you are including a file with the database connection code in it, but the actual file being included is either an older file that doesn't have the setAttribute() statements in it or you have multiple files with the included file name at different paths and the wrong one is being included? short-answer: i can just about guarantee the problem is something your code is or is not doing and we would need to know as much about your code as you do in order to help you with the problem. Quote Link to comment https://forums.phpfreaks.com/topic/293218-pdo-setattribute-and-prepared-statements/#findComment-1500311 Share on other sites More sharing options...
kalster Posted December 21, 2014 Author Share Posted December 21, 2014 this code is included once every php page and at the top of that page. no other page has a connection code in it. The prepared pdo statements follow this connection code. this connection code does not give any errors. try { $dbh = new PDO("mysql:host=$host; charset=utf8", $username, $password); $dbh->exec("CREATE DATABASE `$name`"); $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); } catch(PDOException $e){ echo $e->getMessage(); } Quote Link to comment https://forums.phpfreaks.com/topic/293218-pdo-setattribute-and-prepared-statements/#findComment-1500312 Share on other sites More sharing options...
Solution kalster Posted December 22, 2014 Author Solution Share Posted December 22, 2014 i solved the problem by using this code below the connection try-catch code $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); Quote Link to comment https://forums.phpfreaks.com/topic/293218-pdo-setattribute-and-prepared-statements/#findComment-1500333 Share on other sites More sharing options...
mac_gyver Posted December 22, 2014 Share Posted December 22, 2014 it needs to go immediately after the $dbh = new PDO(...) statement. it's likely that your create table query is failing and is interfering (a php bug perhaps) with the connection try/catch code. why do you even have a query inside your code that's responsible for making the database connection? also, your connection catch block needs to prevent the remainder of the database dependent code from running. if the connection failed. there's no point in trying to run any other database statements. Quote Link to comment https://forums.phpfreaks.com/topic/293218-pdo-setattribute-and-prepared-statements/#findComment-1500334 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.