Skip to content

Instantly share code, notes, and snippets.

@susanndelgado
Last active December 17, 2015 21:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save susanndelgado/5677554 to your computer and use it in GitHub Desktop.
Save susanndelgado/5677554 to your computer and use it in GitHub Desktop.
class created to handle sql queries to a database
<?php
class GeneralDAL {
// Instance variable
private $con = null;
private $tableName = '';
function __construct($url,$userid,$password,$databaseName, $tableName) {
$this->tableName = $tableName;
if ($this->con == null) {
$this->con=mysqli_connect($url,$userid,$password,$databaseName);
}
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
}
function __destruct() {
if ($this->con != null) {
mysqli_close($this->con);
//print "Destroying 3\n";
}
}
function update($columns) {
$allcolumns = "";
$key = "";
$comma = "";
foreach($columns as $x=>$x_value)
{
if($x == 'id')
{
$key = $key . $x . "='$x_value'";
} else {
$allcolumns = "$allcolumns $comma $x = '$x_value'";
$comma = ", ";
}
}
$updateSQL = "UPDATE $this->tableName SET $allcolumns WHERE $key";
mysqli_query($this->con,$updateSQL);
}
function delete($key) {
mysqli_query($this->con,"DELETE FROM $this->tableName WHERE id = $key");
echo "test2";
}
function insert($columns) {
$allColumns = "";
$allValues = "";
$comma = "";
foreach($columns as $x=>$x_value)
{
$allColumns = $allColumns . $comma . $x ;
$comma = ", ";
}
$comma = "";
foreach($columns as $x=>$x_value)
{
$allValues = $allValues . $comma . "'$x_value'" ;
$comma = ", ";
}
$insertSQL = " INSERT INTO $this->tableName ($allColumns) VALUES ($allValues) ";
mysqli_query($this->con, $insertSQL);
}
function select($sql){
$result = mysqli_query($this->con, $sql);
$resultData = [];
while($row = mysqli_fetch_array($result))
{
array_push( $resultData,$row);
}
return $resultData;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment