Share on Google+Share on Google+

PDO Insert

In this tutorial we will study about insertion of data using PDO in PHP, How to insert data using inbuilt function exec() is described here. Examples will make this clearer.


To get data from the database, we need to put some data into it. In PDO we use same conventional coding of inserting data, but to execute the query we use PDO::exec method. PDO::exec method returns the number of effected rows which are successfully updated. PDO::exec method should use when SQL statements wont return a result set. 




try {

$db = new PDO("pgsql:dbname=ignou;host=localhost", "postgres", "1234" );

echo "connected\n";




$sql='insert into student(id,name,roll)values ($id,$name,$roll)';

$count=$db->exec("insert into student(id,name,roll)values ('$id','$name','$roll')");

echo "Number of affected rows are:".$count;



catch (PDOException $e)


echo $e->getMessage();




Number of affected rows are:1




Posted on: April 2, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.