Jump to content

pdo setAttribute and prepared statements.


kalster
Go to solution Solved by kalster,

Recommended Posts

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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();
}
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.