76 lines
1.8 KiB
PHP
76 lines
1.8 KiB
PHP
<?php
|
|
|
|
require_once 'vendor/autoload.php';
|
|
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__ . '/../');
|
|
$dotenv->safeLoad();
|
|
|
|
// Use environment variables for the database password and IP address.
|
|
$db_user = $_ENV['DATABASE_USER'];
|
|
$db_pass = $_ENV['DATABASE_PASSWORD'];
|
|
$ip_addr = $_ENV['IP_ADDRESS'];
|
|
|
|
// Make some constants
|
|
if ($_SERVER['HTTP_HOST'] == 'localhost') {
|
|
define('HOST', 'localhost');
|
|
define('USER', $db_user);
|
|
define('PASS', $db_pass);
|
|
define('DB', 'vintagecoding');
|
|
} else {
|
|
define('HOST', $ip_addr);
|
|
define('USER', $db_user);
|
|
define('PASS', $db_pass);
|
|
define('DB', 'vintagecoding');
|
|
}
|
|
|
|
/*
|
|
* Handles any queries that should have only one row results.
|
|
*/
|
|
function query_one_result($query_stmt)
|
|
{
|
|
$conn = get_connection();
|
|
$results = mysqli_query($conn, $query_stmt);
|
|
mysqli_close($conn);
|
|
|
|
if (mysqli_num_rows($results) == 0 || mysqli_num_rows($results) > 1)
|
|
return null;
|
|
else
|
|
return mysqli_fetch_assoc($results);
|
|
}
|
|
|
|
function query_one_or_more_results($query_stmt)
|
|
{
|
|
$conn = get_connection();
|
|
$results = mysqli_query($conn, $query_stmt);
|
|
mysqli_close($conn);
|
|
|
|
if (mysqli_num_rows($results) == 0)
|
|
return null;
|
|
else
|
|
return $results;
|
|
}
|
|
|
|
/*
|
|
* Executes a statement. Type is typically either 0 or 1, where 0 is an INSERT or UPDATE,
|
|
* and 1 is DELETE. This is to determine whether a boolean value should be returned or
|
|
* the ID of the row(s).
|
|
*/
|
|
function exec_statement($stmt, $type)
|
|
{
|
|
$conn = get_connection();
|
|
$results = mysqli_query($conn, $stmt);
|
|
|
|
if ($type == 0)
|
|
$results = mysqli_insert_id($conn);
|
|
|
|
mysqli_close($conn);
|
|
return $results;
|
|
}
|
|
|
|
function get_connection()
|
|
{
|
|
// Connect to the DB
|
|
$conn = mysqli_connect(HOST, USER, PASS, DB);
|
|
|
|
return $conn;
|
|
}
|