The mysqli class

(PHP 5, PHP 7, PHP 8)

Introduction

Represents a connection between PHP and a MySQL database.

Class synopsis

class mysqli {
/* Properties */
public readonly int|string $affected_rows;
public readonly string $client_info;
public readonly int $client_version;
public readonly int $connect_errno;
public readonly ?string $connect_error;
public readonly int $errno;
public readonly string $error;
public readonly array $error_list;
public readonly int $field_count;
public readonly string $host_info;
public readonly ?string $info;
public readonly int|string $insert_id;
public readonly string $server_info;
public readonly int $server_version;
public readonly string $sqlstate;
public readonly int $protocol_version;
public readonly int $thread_id;
public readonly int $warning_count;
/* Methods */
public __construct(
    ?string $hostname = null,
    ?string $username = null,
    ?string $password = null,
    ?string $database = null,
    ?int $port = null,
    ?string $socket = null
)
public autocommit(bool $enable): bool
public begin_transaction(int $flags = 0, ?string $name = null): bool
public change_user(string $username, string $password, ?string $database): bool
public close(): true
public commit(int $flags = 0, ?string $name = null): bool
public connect(
    ?string $hostname = null,
    ?string $username = null,
    ?string $password = null,
    ?string $database = null,
    ?int $port = null,
    ?string $socket = null
): bool
public debug(string $options): true
public execute_query(string $query, ?array $params = null): mysqli_result|bool
public init(): ?bool
public kill(int $process_id): bool
public more_results(): bool
public multi_query(string $query): bool
public next_result(): bool
public options(int $option, string|int $value): bool
public ping(): bool
public static poll(
    ?array &$read,
    ?array &$error,
    array &$reject,
    int $seconds,
    int $microseconds = 0
): int|false
public query(string $query, int $result_mode = MYSQLI_STORE_RESULT): mysqli_result|bool
public real_connect(
    ?string $hostname = null,
    ?string $username = null,
    ?string $password = null,
    ?string $database = null,
    ?int $port = null,
    ?string $socket = null,
    int $flags = 0
): bool
public real_query(string $query): bool
public refresh(int $flags): bool
public rollback(int $flags = 0, ?string $name = null): bool
public savepoint(string $name): bool
public select_db(string $database): bool
public set_charset(string $charset): bool
public ssl_set(
    ?string $key,
    ?string $certificate,
    ?string $ca_certificate,
    ?string $ca_path,
    ?string $cipher_algos
): true
public stat(): string|false
public store_result(int $mode = 0): mysqli_result|false
public thread_safe(): bool
}

Table of Contents

add a note add a note

User Contributed Notes 2 notes

up
1
user at note dot com
4 years ago
<?php

class Database{

    protected
$url;
    protected
$user;
    protected
$password;
    protected
$db;
    protected
$connection = null;

    public function
__construct($url, $user, $password, $db)
    {
       
$this->url = $url;
       
$this->user = $user;
       
$this->password = $password;
       
$this->db = $db;
    }
    public function
__destruct()
    {
        if(
$this->connection != null){
           
$this->closeConnection();
        }
    }
    protected function
makeConnection(){

       
$this->connection = new mysqli($this->url, $this->user, $this->password, $this->db);
        if(
$this->connection->connect_error){
            echo
"Fail" . $this->connection->connect_error;
        }
    }
    protected function
closeConnection(){
        if(
$this->connection != null){
           
$this->connection->close();
           
$this->connection = null;
        }
    }
    public function
executeQuery($query, $params = null){
       
// Is there a DB connection?
       
$this->makeConnection();
       
// Adjust query with params if available
       
if($params != null){
           
// Change ? to values from parameter array
           
$queryParts = preg_split("/\?/", $query);
           
// if amount of ? is not equal to amount of values => error
           
if(count($queryParts) != count($params) + 1){
                return
false;
            }
           
// Add first part
           
$finalQuery = $queryParts[0];
           
// Loop over all parameters
           
for($i = 0; $i < count($params); $i++){
               
// Add clean parameter to query
               
$finalQuery = $finalQuery . $this->cleanParameters($params[$i]) . $queryParts[$i + 1];
            }
           
$query = $finalQuery;
        }
       
// Check for SQL injection

       
$result = $this->connection->query($query);
        return
$result;
    }
    protected function
cleanParameters($parameters){
       
// prevent SQL injection
       
$result = $this->connection->real_escape_string($parameters);
        return
$result;
    }
}

?>
<?php
include_once("Database.php");

class
DatabaseFactory{
   
// Singleton
   
private static $connection;

    public static function
getDatabase(){
        if(
self::$connection == null){
           
$url = "dt5.ehb.be";
           
$user = "1920WEBINT038";
           
$passw = "94617528";
           
$db = "1920WEBINT038";
           
self::$connection = new Database($url, $user, $passw, $db);
        }
        return
self::$connection;
    }
}

?>
<?php

class Book{
    public
$bookId;
    public
$title;
    public
$releasedate;
    public
$priceExclVAT;
    public
$emailPublisher;

    public function
__construct($bookId, $title, $releasedate, $priceExclVAT, $emailPublisher)
    {
       
$this->bookId = $bookId;
       
$this->title = $title;
       
$this->releasedate = $releasedate;
       
$this->priceExclVAT = $priceExclVAT;
       
$this->emailPublisher = $emailPublisher;
    }

}

?>
up
-8
user at note dot com
4 years ago
<?php

include_once("data/Book.php");
include_once(
"DatabaseFactory.php");
// CRUD methods for book objects
class BookDB{

    private static function
getConnection(){
        return
DatabaseFactory::getDatabase();
    }

    public static function
getAll(){
       
$query = "SELECT * FROM Book";
       
// Execute query
       
$conn = self::getConnection();
       
$results = $conn->executeQuery($query);

       
$resultsArray = array();

        for(
$i = 0; $i < $results->num_rows; $i++){
           
$row = $results->fetch_array(); // Retrieves the current selected row

            // Make a book
           
$book = self::convertRowToObject($row);

           
// Add book to resultsArray
           
$resultsArray[$i] = $book;
        }
        return
$resultsArray;
    }

    public static function
convertRowToObject($row){
        return new
Book(
           
$row["BookId"],
           
$row["Title"],
           
$row["Releasedate"],
           
$row["Price"],
           
$row["EmailPublisher"]
        );
    }

    public static function
insert($book){
        return
self::getConnection()->executeQuery("INSERT INTO Book(Title, Releasedate, Price, EmailPublisher) VALUES ('?', '?', '?', '?')", array($book->title, $book->releasedate, $book->priceExclVAT, $book->emailPublisher));
    }

}

?>
To Top