Last active
December 23, 2017 11:47
-
-
Save motyar/2f885cb13d79294862054ff072b00001 to your computer and use it in GitHub Desktop.
PHP database class.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class db { | |
var $host; | |
var $db_name; | |
var $username; | |
var $password; | |
var $res; | |
var $query; | |
function __construct($host,$db_name,$username,$password){ | |
$this->host =$host; | |
$this->password = $password; | |
$this->username = $username; | |
$this->db_name = $db_name; | |
} | |
function connect(){ | |
$this->res = mysqli_connect($this->host,$this->username,$this->password, $this->db_name); | |
if(!$this->res) | |
die($this->debug("connect")); | |
} | |
function rowcount($query){ | |
$this->query = $query; | |
$result = mysqli_query($this->res,$query) or die($this->debug("query")); | |
return mysqli_num_rows($result); | |
} | |
function debug($type) { | |
switch ($type) { | |
case "connect": | |
$message = "MySQL Error Occured"; | |
$result = mysqli_errno() . ": " . mysql_error(); | |
$output = "Could not connect to the database. Be sure to check that your database connection settings are correct and that the MySQL server in running."; | |
break; | |
case "database": | |
$message = "MySQL Error Occurred"; | |
$result = mysqli_errno($this->res) . ": " . mysqli_error($this->res); | |
$output = "Sorry an error has occured accessing the database. Be sure to check that your database connection settings are correct and that the MySQL server in running."; | |
case "query": | |
$message = "MySQL Error Occurred"; | |
$result = mysqli_errno($this->res) . ": " . mysqli_error($this->res); | |
$query = $this->query; | |
} | |
return $result.$query; | |
} | |
function Q($type, $query){ | |
$this->query = $query; | |
if($type=="select"){ | |
$result = mysqli_query($this->res, $query) or die($this->debug("query")); | |
if(mysqli_num_rows($result) > 0){ | |
while($record = mysqli_fetch_array($result)){ | |
$data[] = $record; | |
} | |
return $data; | |
}else{ | |
return false; | |
} | |
}else if($type=="insert") { | |
$result = mysqli_query($this->res,$query) or die($this->debug("query")); | |
return mysqli_insert_id($this->res); | |
}else if($type=="update"){ | |
$result = mysqli_query($this->res,$query) or die($this->debug("query")); | |
return $result; | |
}else if($type == "delete"){ | |
$result = mysqli_query($this->res,$query) or die($this->debug("query")); | |
return $result; | |
} | |
return false; | |
} | |
} | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once("db.php"); | |
require_once("top.php"); | |
// $rows = $db->Q("select","SELECT * FROM `table`"); | |
// $db->Q("insert","INSERT into.... `table`"); | |
// $db->Q("update","UPDATE `table`..."); | |
// $db->Q("delete", "DELETE..."); | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
global $db; | |
$db = new databaseConnection($host,$dbname,$username,$password); | |
$db->connect(); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment