// Press the 'Run' button on the top to start the web server,
// then click the URL that is emitted to the Output tab of the console
echo 'Tutorial using PHP and PDO';
//variables to establish connection
$server = getenv('IP');
$pass = "";
$user = getenv('C9_USER');
$db = "c9";
//Try catch block for SQL statements
try{
//String to create & connect to database
$connect = new PDO("mysql:host=$server;dbname=$db", $user, $pass);
$connect->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
echo "
Created & Connected";
//String to create table
$line = "create table hello_world ( id int(2) auto_increment primary key,
string varchar(50) not null,
value varchar(10));";
//$line = "drop table if exists hello_world";
$connect->exec($line);
echo "
Table created";
//Prepare for multiple entries
$line2 = $connect->prepare("insert into hello_world(string, value)
values (:string, :value)");
$line2->bindParam(':string', $str);
$line2->bindParam(':value', $val);
$str = 'Obioku Obotette';
$val = 'Name';
$line2->execute();
$str = 'Another User';
$val = 'Blank';
$line2->execute();
echo "
Lines inserted into table";
//retrieve data
$line3 = $connect->prepare("select * from hello_world");
$line3->execute();
//$print = $line3->setFetchMode(PDO::FETCH_ASSOC);
foreach($line3->fetchAll() as $show){
echo "
";
print_r($show);
}
//Catch exception
}catch(PDOException $e)
{
echo "
Error Occurred.".$e->getMessage();
}
?>
No comments:
Post a Comment
Thank you very much for viewing this entry and I hope you are able to return soon to continue to enjoy more of the site.
Please share your thoughts in the comment section.
Be blessed and enjoy life!