Share on Google+Share on Google+
PDO Execute
Posted on: April 2, 2010 at 12:00 AM
Advertisement
In this tutorial we will study about PDO Execution of code (select) in PHP, how to display data using PDO, etc. Examples will make this clearer.

PDO Execute Code:

In the current tutorial we will study how to execute a simple SQL code using PDO. In the previous tutorials we knew that how to connect with database. Now we will see how to make queries and store, update, retrieve data. You can create a database by your own, assume any number of fields, data type, constraints etc. and fill with some data, after that access the database and make some queries using the following coding. In most of the PHP examples we have used MySQL, in this tutorial we are going to demonstrate the example PostgreSQL, along with connectivity, and closing the database. One point we should keep remember that the coding for connectivity with different databases are different but rest of the coding will remain same, for example select, update, delete.

Example:

<?php

try {

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

$sql='select * from student';

foreach($db->query($sql)as $row)

{

echo "<table border=1>";

echo "<tr><td>Name </td><td>Roll</td>";

echo "<tr><td>$row[1] </td><td>".$row[2]."</td>";

echo "</table>";

}

$db=null;

}

catch (PDOException $e)

{

echo $e->getMessage();

}

?>

 

Output:

Name Roll
rose 1

Advertisement

Related Tags for PDO Execute:


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.

Posted on: April 2, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com