Translate To Preferred Language

Search ObiokusThoughts

Please Read Today's Featured Post

Reading Brashly

Involve Evolve Revolve Devolve Aesop Dr. Seuss Father time  Mother goose Fast danger Nurture nature Rainbow Sunshine ...

HelloWorld Example in PHP (some bugs may occur)


// 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);
    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 for reading.
Please share your thoughts.
Be blessed and enjoy life!