Error not raised while executing wrong query

Hi all,

In a previous question (http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28646127.html), I created a table with the column "title".
To check what happens when there is an error during the SQL execution I change to "titl" the name of the previous column.

When running the following script, no error is raised and the SQL query really failed.

Why there is no error message?

My script
<?php	

include("data-connection.inc.php");

$sql  = "INSERT INTO `pouxdagoalyne`.`TEACHINGS` (`title`, `location`, `teaching_date`, `description`, `opening_hours`) ";
$sql .= "VALUES (:title, :location, :teaching_date, :description, :opening_hours)";
try
{
	$sth = $dbh->prepare($sql);
}
catch(PDOException $e)
{
    var_dump($e);
}

try
{
	$sth->execute(array(':title' => $_POST['teaching-title'], ':location' => $_POST['teaching-location'], ':teaching_date' => $_POST['teaching-when'], ':description' => $_POST['teaching-description'], ':opening_hours' => $_POST['teaching-opening-hours']));   
}
catch(PDOException $e)
{
    die($e->Message);
}
?>
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset=utf-8>
<meta name="viewport" content="width=620">
<title>Teaching lesson creation</title>
<link rel="stylesheet" href="">
<script src=""></script>
</head>
<body>
<h1>Create a session: <?php echo $_POST['teaching-title']; ?></h1>
<p>The session <em><?php echo $_POST['teaching-title'] ; ?></em> has been created.</p>
</body>
</html>

Open in new window

LVL 1
Richard Coffree-commerce Product ManagerAsked:
Who is Participating?
 
AlexanderREnterprise Web DeveloperCommented:
Ok, that at least rules out syntax error.

Next step is to configure PDO to run in proper mode.

$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

Add those two as soon as possible after the connection.  First options makes pdo use exceptions, as opposed to being silent or output error codes in its return. Second attribute may have to be switched between true and false depending on your driver. If drivers supports actual prepared statements then bad column name will be detected during preparation stage, as opposed to at the end in execution. If driver does not support prepares and get errors from second attribute make it "true".
0
 
AlexanderREnterprise Web DeveloperCommented:
die($e->Message);

should be

die($e->getMessage());
0
 
Richard Coffree-commerce Product ManagerAuthor Commented:
@AlexanderR, it has changed nothing :-(
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.