/////////////////////////////////////////////Task10 /////////////////////////////////////////
ECHO '
Task 10: MySQL PDO
';
//Server connection details
//I have decided to create the database and tables via code.
//thus I first connect to the server then to the database.
$cnstr = 'mysql:host=localhost;';
$uname = 'lunexffg_draad';
$pword = 'draad123';
//Connect to the database server.
try{
$db = new pdo($cnstr,$uname,$pword);
} catch (Exception $e){
$errmsg = $e->getMessage();
echo "An error occurred while connecting to the server: $errmsg";
die;
}
function setupDB(&$db){
$strSql = 'CREATE DATABASE IF NOT EXISTS lunexffg_main';
Echo 'Check/Create database: ';
//Execute the create check db
if ($db->exec($strSql)){
Echo 'Successfull.
';
}else{
Echo 'Failed.
';
}
//Connecto to database
try{
$db = new pdo($GLOBALS['cnstr'] .'dbname=lunexffg_main' ,$GLOBALS['uname'],$GLOBALS['pword']);
} catch (Exception $e){
$errmsg = $e->getMessage();
echo "An error occurred while connecting to the database: $errmsg";
die;
}
Echo 'Connection to Database Successfull.
';
$strSql = 'CREATE Table IF NOT EXISTS tblTask10(ID int not null Primary key,
GENDER varchar(1) not null,AGE int,EMPLOYED varchar(3) not null)';
Echo 'Check/Create tblTask10: ';
//Execute the create check Table
$db->exec($strSql);
$strSql = "SHOW TABLES LIKE 'tblTask10'";
$resultPDO = $db->query($strSql);
$result = $resultPDO->fetch();
if ($result == null){
Echo 'an unforseen database error prevented access to the table.
';
die;
}else{
$strSql = "Select * from tblTask10";
$resultPDO = $db->query($strSql);
$result = $resultPDO->fetchAll();
if(count($result) == 0){
Echo 'Successfully created the table.
';
$strSql = "INSERT INTO tblTask10 (`ID`, `GENDER`, `AGE`, `EMPLOYED`) values
(1,'M',37,'YES'),(2,'M',63,'YES'),(3,'F',22,'YES'),(4,'F',22,'NO'),
(5,'M',34,'YES'),(6,'F',21,'NO'),(7,'F',25,'YES'),(8,'F',42,'NO'),
(9,'M',33,'NO'),(10,'M',27,'YES'),(11,'F',24,'YES'),(12,'M',39,'NO'),
(13,'M',31,'YES'),(14,'M',28,'YES'),(15,'F',52,'YES'),(16,'F',55,'NO')";
$result = $db->exec($strSql);
Echo "Successfully added $result records.
";
}else{
Echo 'Successfull with '. count($result) .' rows.
';
}
}
Echo '
';
}
function outputPDO_Results($ResultArray){
foreach($ResultArray as $result){
Echo "ID: ". $result['ID'] .", GENDER: ". $result['GENDER'] .", AGE: ". $result['AGE'] .", EMPLOYED: ". $result['EMPLOYED'] ."
";
}
Echo count($ResultArray) .' Rows printed.
';
}
function fetchAll_Method($db){
Echo "FetchAll Method: All records returned.
";
Echo "--------------------------------------------------------------------------
";
$strSql = "SELECT * FROM tblTask10";
$returnPDO = $db->query($strSql);
outputPDO_Results($returnPDO->fetchAll());
Echo "--------------------------------------------------------------------------
";
}
function Select_Named($db,$min_age,$sex){
Echo "Named Select: where Minimum age is $min_age and Gender is $sex
";
Echo "--------------------------------------------------------------------------
";
$strSql = "SELECT * FROM tblTask10
WHERE AGE >= :min_age
AND GENDER = :sex";
$prep = $db->prepare($strSql);
$prep->bindValue(':min_age', $min_age);
$prep->bindValue(':sex', $sex);
$prep->execute();
outputPDO_Results($prep->fetchAll());
$prep->closeCursor();
Echo "--------------------------------------------------------------------------
";
}
function Select_QuestionMark($db,$min_age,$sex){
Echo "Question Mark Select: where Minimum age is $min_age and Gender is $sex
";
Echo "------------------------------------------------------------------------------------
";
$strSql = "SELECT * FROM tblTask10
WHERE AGE >= ?
AND GENDER = ?";
$prep = $db->prepare($strSql);
$prep->bindValue(1, $min_age);
$prep->bindValue(2, $sex);
$prep->execute();
outputPDO_Results($prep->fetchAll());
$prep->closeCursor();
Echo "------------------------------------------------------------------------------------
";
}
setupDB($db);
fetchAll_Method($db);
Select_Named($db,35,'M');
Select_QuestionMark($db,30,'F');