HelloWorld example with PHP & PDO

Obioku Obotette
Oct 22, 2020

// A simple web site in Cloud9 that runs through Apache
// 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
//String to create & connect to database
$connect = new PDO(“mysql:host=$server;dbname=$db”, $user, $pass);
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”;
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’;

$str = ‘Another User’;
$val = ‘Blank’;

echo “
Lines inserted into table”;

//retrieve data
$line3 = $connect->prepare(“select * from hello_world”);
//$print = $line3->setFetchMode(PDO::FETCH_ASSOC);
foreach($line3->fetchAll() as $show){
echo “
//Catch exception
}catch(PDOException $e)
echo “
Error Occurred.”.$e->getMessage();



Obioku Obotette

Trying to introduce myself in a new forum with hopeful expectations!