connection = new mysqli($host, $username, $password, $databasename);
$this->tablename = "films";
$this->indexfield = "fcode";
$this->indexdelim = "'";
}
function __destruct() {
$this->connection->close();
}
function __get($name) {
return $this->$name;
}
function __set($name, $value) {
$this->$name = $value;
}
function listFilms() {
$retval = "
MySql Film Code | Title | Id | Date | Kind | Length |
";
$result = $this->connection->query("SELECT * FROM " . $this->tablename);
while ($row = $result->fetch_array( MYSQLI_NUM )) {
$retval .= "$row[0] | $row[1] | $row[2] | $row[3] | $row[4] | $row[5] |
";
}
$result->close();
$retval .= "
";
return $retval;
}
function addFilm($code = "", $title = "", $did = "", $date_prod = "", $kind = "", $len = "") {
$this->sql = "";
if ($code != "") {
$this->result = $this->connection->query("INSERT INTO " . $this->tablename . " (" . $this->indexfield . ",title,anid,pdate,fkind,flen) values (" . $this->indexdelim . $code . $this->indexdelim . ",'" . $title . "'," . $did . ",'" . $date_prod . "','" . $kind . "','" . $len . "')");
}
}
function removeFilm($which) {
$this->sql = "DELETE from " . $this->tablename . " WHERE " . $this->indexfield . "=" . $this->indexdelim . $which . $this->indexdelim;
$this->result = $this->connection->query($this->sql);
}
function amendFilm($code = "", $title = "", $did = "", $date_prod = "", $kind = "", $len = "") {
$this->sql = "";
if ($code != "") {
$delim=" ";
$this->sql = "UPDATE " . $this->tablename . " SET WHERE " . $this->indexfield . "=" . $this->indexdelim . $code . $this->indexdelim;
if (isset($_GET['title']) && ($_GET['title'] != "")) {
$this->sql = str_replace(" WHERE ", $delim . " title='" . $title . "'" . " WHERE ", $this->sql);
$delim = ", ";
}
if (strpos($did,"-") === false && ($did != "")) {
$this->sql = str_replace(" WHERE ", $delim . " anid=" . $did . " WHERE ", $this->sql);
$delim = ", ";
}
if (($date_prod != "")) {
//$this->sql = str_replace(" WHERE ", $delim . " pdate=to_date('" . $date_prod . "','YYYY-MM-DD')" . " WHERE ", $this->sql);
$this->sql = str_replace(" WHERE ", $delim . " pdate='" . $date_prod . "'" . " WHERE ", $this->sql);
$delim = ", ";
}
if (($kind != "")) {
$this->sql = str_replace(" WHERE ", $delim . " fkind='" . $kind . "'" . " WHERE ", $this->sql);
$delim = ", ";
}
if (($len != "")) {
$this->sql = str_replace(" WHERE ", $delim . " flen='" . $len . "'" . " WHERE ", $this->sql);
$delim = ", ";
}
}
if ($this->sql != "") {
$this->result = $this->connection->query($this->sql);
}
}
}
class PostgreSQLDatabase implements Database {
public $connection;
public $tablename = "films";
public $indexfield = "code";
public $indexdelim = "'";
public $result;
public $sql;
function __construct($host, $databasename, $username, $password, $port = 5432) {
$this->connection = new PDO('pgsql:host=' . $host . ';port=' . $port . ';dbname=' . $databasename . ';user=' . $username . ';password=' . $password);
$this->tablename = "films";
$this->indexfield = "code";
$this->indexdelim = "'";
}
function __destruct() {
$this->connection->close();
}
function __get($name) {
return $this->$name;
}
function __set($name, $value) {
$this->$name = $value;
}
function listFilms() {
$retval = "PostgreSQL Film Code | Title | Id | Date | Kind | Length |
";
foreach ($this->connection->query("SELECT * FROM " . $this->tablename) as $row) {
$retval .= "$row[0] | $row[1] | $row[2] | $row[3] | $row[4] | $row[5] |
";
}
$retval .= "
";
return $retval;
}
function addFilm($code = "", $title = "", $did = "", $date_prod = "", $kind = "", $len = "") {
$this->sql = "";
if ($code != "") {
$this->result = $this->connection->query("INSERT INTO " . $this->tablename . " (" . $this->indexfield . ",title,did,date_prod,kind,len) values (" . $this->indexdelim . $code . $this->indexdelim . ",'" . $title . "'," . $did . ",to_date('" . $date_prod . "','YYYY-MM-DD'),'" . $kind . "','" . $len . "')");
}
}
function removeFilm($which) {
$this->sql = "DELETE from " . $this->tablename . " WHERE " . $this->indexfield . "=" . $this->indexdelim . $which . $this->indexdelim;
$this->result = $this->connection->query($this->sql);
}
function amendFilm($code = "", $title = "", $did = "", $date_prod = "", $kind = "", $len = "") {
$this->sql = "";
if ($code != "") {
$delim=" ";
$this->sql = "UPDATE " . $this->tablename . " SET WHERE " . $this->indexfield . "=" . $this->indexdelim . $code . $this->indexdelim;
if (isset($_GET['title']) && ($_GET['title'] != "")) {
$this->sql = str_replace(" WHERE ", $delim . " title='" . $title . "'" . " WHERE ", $this->sql);
$delim = ", ";
}
if (strpos($did,"-") === false && ($did != "")) {
$this->sql = str_replace(" WHERE ", $delim . " did=" . $did . " WHERE ", $this->sql);
$delim = ", ";
}
if (($date_prod != "")) {
$this->sql = str_replace(" WHERE ", $delim . " date_prod=to_date('" . $date_prod . "','YYYY-MM-DD')" . " WHERE ", $this->sql);
$delim = ", ";
}
if (($kind != "")) {
$this->sql = str_replace(" WHERE ", $delim . " kind='" . $kind . "'" . " WHERE ", $this->sql);
$delim = ", ";
}
if (($len != "")) {
$this->sql = str_replace(" WHERE ", $delim . " len='" . $len . "'" . " WHERE ", $this->sql);
$delim = ", ";
}
}
if ($this->sql != "") {
$this->result = $this->connection->query($this->sql);
}
}
}
// Start of execution code ...
echo "PostgreSQL or MySql Form - RJM Programming - June, 2015PostgreSQL or MySql Form - RJM Programming - May, 2015
";
$mydbtype = "PostgreSQL";
$myaltdbtype = "MySql";
if ((isset($_GET['MySql']) || isset($_POST['MySql'])) && (isset($_GET['PostgreSQL']) || isset($_POST['PostgreSQL']))) {
if (($_GET['MySql'] . $_POST['MySql']) != '') {
$database = new MySqlDatabase("localhost", "filmsdb", "username", "password");
$myaltdbtype = "PostgreSQL";
$mydbtype = "MySql";
} else {
$database = new PostgreSQLDatabase("localhost", "mydatabase", "username", "password");
}
} else if (isset($_GET['MySql']) || isset($_POST['MySql'])) {
$database = new MySqlDatabase("localhost", "filmsdb", "username", "password");
$myaltdbtype = "PostgreSQL";
$mydbtype = "MySql";
} else {
$database = new PostgreSQLDatabase("localhost", "mydatabase", "username", "password");
}
if (isset($_GET['code']) && ($_GET['code'] != "") && isset($_GET['delete'])) {
$database->removeFilm($_GET['code']);
} else if (isset($_GET['code']) && ($_GET['code'] != "") && strpos($_GET['did'],"-") === false && isset($_GET['title']) && isset($_GET['did']) && isset($_GET['date_prod']) && isset($_GET['kind']) && isset($_GET['len']) && ($_GET['title'] != "") && ($_GET['did'] != "") && ($_GET['date_prod'] != "") && ($_GET['kind'] != "") && ($_GET['len'] != "")) {
$database->addFilm($_GET['code'],$_GET['title'],$_GET['did'],$_GET['date_prod'],$_GET['kind'],$_GET['len']);
} else if (isset($_GET['code']) && ($_GET['code'] != "")) {
$database->amendFilm($_GET['code'],$_GET['title'],$_GET['did'],$_GET['date_prod'],$_GET['kind'],$_GET['len']);
}
echo $database->listFilms();
echo "