Created
January 14, 2021 00:55
-
-
Save jpneey/c9f5c9c752a47069c50aa87f377d18e9 to your computer and use it in GitHub Desktop.
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 DBHandler { | |
private $host = "hostname"; | |
private $user = "database_user"; | |
private $password = "database_password"; | |
private $database = "database_name"; | |
private $conn; | |
function __construct() { | |
$this->conn = $this->connectDB(); | |
if(!$this->conn) { | |
echo "Connecting to database failed!"; | |
} | |
} | |
public function connectDB() { | |
$conn = new mysqli($this->host,$this->user,$this->password,$this->database); | |
return $conn; | |
} | |
function numRows($conn, $query) { | |
$result = mysqli_query($conn,$query); | |
$rowcount = mysqli_num_rows($result); | |
return $rowcount; | |
} | |
function runQuery($conn, $query) { | |
$result = mysqli_query($conn,$query); | |
while($row=mysqli_fetch_assoc($result)) { | |
$resultset[] = $row; | |
} | |
if(!empty($resultset)) | |
return $resultset; | |
} | |
public function prepareQuery($connection, $query, $types = "", $values = array()) { | |
$stmt = $connection->prepare($query); | |
if(!empty($types) && !empty($values)) { | |
$stmt->bind_param($types, ...$values); | |
} | |
return $stmt; | |
} | |
public function fetchAssoc($stmt){ | |
$stmt->execute(); | |
$response = $stmt->get_result()->fetch_all(MYSQLI_ASSOC); | |
$stmt->close(); | |
return $response; | |
} | |
public function fetchRow($stmt){ | |
$stmt->execute(); | |
$response = $stmt->get_result()->fetch_row(); | |
$stmt->close(); | |
return $response; | |
} | |
public function fetchCount($stmt){ | |
$stmt->execute(); | |
$response = $stmt->get_result()->num_rows; | |
$stmt->close(); | |
return $response; | |
} | |
public function execute($stmt){ | |
if($stmt->execute()) { | |
return true; | |
} | |
$error = "Error: ". $stmt->error; | |
return $error; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment