PDO - Show message if inserted successfully in MySQL
Posted: 13 Jan 2015, 13:50
I wrote a PHP code to insert some data in to MySQL database. I want to display a message indicating whether the records are successfully added to the database or not.
So, how can I check if the Insert was successfully performed without making another SQL query?
Code:
So, how can I check if the Insert was successfully performed without making another SQL query?
Code:
Code: Select all
<?php
try {
$db_user = 'root';
$db_pass = 'pass';
$db = new PDO( 'mysql:host=localhost;dbname=the_db', $db_user, $db_pass );
$name = 'some_name';
$email = 'some@mail.com';
$sql = "INSERT INTO table_name (name, email) VALUES (:name, :email)";
$query = $db->prepare($sql);
$query->execute(array(':name'=>$name, ':email'=>$email));
}
catch(PDOException $e) {
echo $e->getMessage();
}