Jump to content

First PDO query


Q695

Recommended Posts

What's wrong with the query, I'm not getting errors outputted:
// configuration
$dbtype     = "MySQL";
$dbhost     = "localhost";
$dbname     = "eagle";
$dbuser     = "root";
$dbpass     = "";


// database connection
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);
// query
$sql = "INSERT INTO member_data (fname, mname, lname, street, apt, city, state, zip, email, number, job, awarded) VALUES
(:fname, :mname, :lname, :street, :apt, :city, :state, :zip, :email, :number, :job, :awarded)
";
echo $sql;
$q = $conn->prepare($sql);
$q->execute(array(':fname'=>$fname,
                  ':mname'=>$mname,
                  ':lname'=>$lname,
                  ':street'=>$street,
                  ':apt'=>$apt,
                  ':city'=>$city,
                  ':state'=>$state,
                  ':zip'=>$zip,
                  ':email'=>$email,
                  ':number'=>$number,
                  ':job'=>$job,
                  ':awarded'=>$awarded
                  ));

How do I run an or die to output the error?

Link to comment
https://forums.phpfreaks.com/topic/289242-first-pdo-query/
Share on other sites

it's easiest to use exceptions with pdo statements to catch errors and handle logging/displaying error messages. the connection always throws an exception if it fails and once you have created a connection, you can configure the pdo instance to throw exceptions, which you can use a try/catch block around to handle, for all other statements that fail.

 

see this link on how to use a try/catch block for the connection and how to configure pdo statements to throw exceptions - http://us3.php.net/manual/en/pdo.error-handling.php

Link to comment
https://forums.phpfreaks.com/topic/289242-first-pdo-query/#findComment-1483025
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.