Skip to content

Instantly share code, notes, and snippets.

@viralgh
Created July 1, 2020 11:00
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 viralgh/6a0852aaae0e040bf8185f60c0daf6a8 to your computer and use it in GitHub Desktop.
Save viralgh/6a0852aaae0e040bf8185f60c0daf6a8 to your computer and use it in GitHub Desktop.
<?php
// Database credentials
$_servername = "localhost";
$_username = "root";
$_password = "";
$_dbname = "location_db";
// Create connection
$_conn = @new mysqli($_servername, $_username, $_password, $_dbname);
// Check connection
if ($_conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// For getting multiple records from database
function get_records($query = "") {
global $_conn;
$result = $_conn->query($query);
if (!$result) {
die("Error: " . $_conn->error);
}
$query_result = [];
while ($row = $result->fetch_assoc()) {
$query_result[] = $row;
}
return $query_result;
}
// For getting single record from database
function get_record($query = "") {
global $_conn;
$result = $_conn->query($query);
if (!$result) {
die("Error: " . $_conn->error);
}
return $result->fetch_assoc();
}
// For fireing query from database
function fire_query($query = "") {
global $_conn;
$result = $_conn->query($query);
if (!$result) {
die("Error: " . $_conn->error);
}
return $_conn->affected_rows;
}
@viralgh
Copy link
Author

viralgh commented Jul 1, 2020

$_conn->close();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment