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
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)…

--

--

Obioku Obotette
0 Followers

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