Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Wrapper around PHP's Prepared Statements.
- *
- * 2018
- *
- * More info: http://php.net/manual/en/mysqli.quickstart.prepared-statements.php
- */
- class Database {
- /**
- * Change the below variables to match your host name, database name, user
- * name, and password respectively.
- */
- private $host = "localhost";
- private $name = "<redacted>";
- private $user = "<redacted>";
- private $pass = "<redacted>";
- /**
- * Other variables
- */
- private $connection = NULL;
- public $connected = false;
- public $error = "";
- /**
- * Tries to connect and returns whether the attempt was succesful
- */
- function connect() {
- if ($this->connected === true) {
- $this->error = "";
- return true;
- }
- $this->connection = new mysqli($this->host, $this->user, $this->pass, $this->name);
- // has the connection failed
- if ($this->connection->connect_errno !== 0) {
- // connection failed, so set the error and connection
- // status
- $this->error = $this->connection->connect_error;
- $this->connected = false;
- $this->connection->close();
- } else {
- // successful, so unset the error and set the connected
- // flag
- $this->error = "";
- $this->connected = true;
- }
- return $this->connected;
- }
- /**
- * Returns false on failure
- */
- function disconnect() {
- $this->connection = null;
- // $this->connection->close();
- $this->connected = false;
- $this->error = "";
- return true;
- }
- /**
- * Performs a query with types and returns the result or false on
- * failure.
- * See
- *
- * Ex. $db->query("SELECT * FROM things WHERE id = ?", "i", $id);
- */
- function query(string $query, string $types = null, ...$params) {
- $this->error = "";
- if (!$this->connected) {
- return false;
- }
- if (is_null($types) || count($params) === 0) {
- $result = $this->connection->query($query);
- } else {
- // prepare query
- $statement = $this->connection->prepare($query);
- if (!$statement) {
- return false;
- }
- // bind params
- if (!$statement->bind_param($types, ...$params)) {
- return false;
- }
- // execute
- if (!$statement->execute()) {
- return false;
- }
- $result = $this->get_result($statement);
- }
- if (!$result) {
- return false;
- }
- return $result;
- }
- function get_result($statement) {
- $ret = new fake_mysqli_result($statement);
- if (!$ret)
- return null;
- return $ret;
- }
- }
- /** EXPLANATION:
- * We are creating a fake "result" structure to enable us to have
- * source-level equivalent syntax to a query executed via
- * mysqli_query().
- **/
- class fake_mysqli_result {
- public $statement, $field_count;
- function __construct($statement) {
- $this->statement = $statement;
- $metadata = $statement->result_metadata();
- if ($metadata === false) {
- return null;
- }
- $this->field_count = $metadata->field_count;
- $metadata->free_result();
- }
- function fetch_array() {
- $ret = array_fill(0, $this->field_count, null);
- if (!$this->statement->bind_result(...$ret)) {
- return null;
- }
- // This should advance the "$this->statement" cursor.
- if (!$this->statement->fetch()) {
- return null;
- }
- // Return the array we built.
- return $ret;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement