Last active
March 27, 2024 11:07
-
-
Save foxy4096/66148571320ea693a8596d9754405d9a to your computer and use it in GitHub Desktop.
✨ A simple database wrapper 📦
This file contains 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 | |
$GLOBALS['conn'] = $dbconn; // Replace it with your own `mysqli_connect` :) | |
function db_query($sql, $params = array(), $single = true) | |
{ | |
$stmt = mysqli_prepare($GLOBALS['conn'], $sql); | |
if (!$stmt) { | |
// Handle SQL query preparation error | |
return null; | |
} | |
// Determine parameter types dynamically based on the values passed | |
$types = ''; | |
foreach ($params as $param) { | |
if (is_int($param)) { | |
$types .= 'i'; // Integer | |
} elseif (is_float($param)) { | |
$types .= 'd'; // Double | |
} elseif (is_string($param)) { | |
$types .= 's'; // String | |
} else { | |
$types .= 'b'; // Blob | |
} | |
} | |
// Bind parameters with dynamically determined types to the prepared statement | |
if (!empty($params)) { | |
mysqli_stmt_bind_param($stmt, $types, ...$params); | |
} | |
// Execute the prepared statement | |
$success = mysqli_stmt_execute($stmt); | |
if (!$success) { | |
// Handle SQL query execution error | |
return null; | |
} | |
$result = mysqli_stmt_get_result($stmt); | |
$affected_rows = mysqli_stmt_affected_rows($stmt); | |
if ($result === false) { | |
if ($affected_rows > 0){ | |
return mysqli_stmt_insert_id($stmt); | |
} | |
// Handle result retrieval error | |
return null; | |
} | |
if ($single) { | |
$row = mysqli_fetch_assoc($result); | |
} else { | |
$row = mysqli_fetch_all($result, MYSQLI_ASSOC); | |
} | |
mysqli_stmt_close($stmt); | |
return $row; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment