Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // file CallCenter.php
- <?php
- class CallCenter{
- private $dbi = false;
- public function connectDB()
- {
- $db = new Database();
- $this->dbi = $db->connect();
- }
- public function __construct()
- {
- $this->connectDB();
- }
- public function main()
- {
- do{
- $input_str = trim(fgets(STDIN));
- $res = $this->getCountryInfo($input_str);
- if($res != false){
- $flag = false;
- foreach ($res as $i=>$iv){
- echo "Country: " . $iv['country_name'] . "\r\n";
- echo "Capital: " . $iv['capital'] . "\r\n";
- $flag = true;
- break;
- }
- if($flag == false){
- echo "Country not found.";
- }
- }else{
- echo "Could not read from DB.";
- }
- }
- while($input_str != 'Bye');
- }
- private function getCountryInfo($str)
- {
- $result = $this->dbi->query("
- SELECT `country_name`, `capital`
- FROM `countries`
- WHERE `country_name` = \"$str\"
- OR `country_code` = \"$str\"
- OR `iso_code` = \"$str\"
- LIMIT 0,1 ");
- if (is_object($result)){
- return($result);
- }
- else{
- return false;
- }
- }
- }
- // file Database.php
- <?php
- class Database extends PDO
- {
- private $db_host = "localhost";
- private $db_name = "geography";
- private $db_user = "root";
- private $db_password = "";
- private $db = false;
- public function __construct()
- {
- parent::__construct("mysql:dbname=" . $this->db_name . ";host=" . $this->db_host, $this->db_user, $this->db_password);
- }
- public function setErrorExeption()
- {
- $this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- }
- public function connect()
- {
- $this->setErrorExeption();
- return $this;
- }
- }
- // file callcenter_app.php
- <?php
- include 'Database.php';
- include 'CallCenter.php';
- $app = new CallCenter();
- $app->main();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement