PHP 7.4.3 released

A classe MySQLi

Introdução

Representa uma conexão entre o PHP e um banco de dados MySQL.

Sinopse da classe

MySQLi {
/* Propriedades */
int $affected_rows;
int $errno;
string $error;
string $host_info;
string $protocol_version;
string $server_info;
int $server_version;
string $info;
int $insert_id;
/* Métodos */
mysqli_affected_rows ( mysqli $link ) : int
mysqli::autocommit ( bool $mode ) : bool
public mysqli::begin_transaction ([ int $flags = 0 [, string $name ]] ) : bool
mysqli::change_user ( string $user , string $password , string $database ) : bool
mysqli::character_set_name ( void ) : string
mysqli::commit ( void ) : bool
mysqli::connect ([ string $host = ini_get("mysqli.default_host") [, string $username = ini_get("mysqli.default_user") [, string $passwd = ini_get("mysqli.default_pw") [, string $dbname = "" [, int $port = ini_get("mysqli.default_port") [, string $socket = ini_get("mysqli.default_socket") ]]]]]] ) : void
mysqli::dump_debug_info ( void ) : bool
mysqli_errno ( mysqli $link ) : int
mysqli_error_list ( mysqli $link ) : array
mysqli_error ( mysqli $link ) : string
mysqli::get_charset ( void ) : object
mysqli::get_client_info ( void ) : string
mysqli_get_host_info ( mysqli $link ) : string
mysqli_get_proto_info ( mysqli $link ) : int
mysqli_get_server_info ( mysqli $link ) : string
mysqli_get_server_version ( mysqli $link ) : int
mysqli_info ( mysqli $link ) : string
mysqli_init ( void ) : mysqli
mysqli_insert_id ( mysqli $link ) : int
mysqli::kill ( int $processid ) : bool
mysqli::more_results ( void ) : bool
mysqli::multi_query ( string $query ) : bool
mysqli::next_result ( void ) : bool
mysqli::options ( int $option , mixed $value ) : bool
mysqli::ping ( void ) : bool
public static mysqli::poll ( array &$read , array &$error , array &$reject , int $sec [, int $usec = 0 ] ) : int
mysqli::prepare ( string $query ) : mysqli_stmt
mysqli::query ( string $query [, int $resultmode = MYSQLI_STORE_RESULT ] ) : mixed
mysqli::real_connect ([ string $host [, string $username [, string $passwd [, string $dbname [, int $port [, string $socket [, int $flags ]]]]]]] ) : bool
mysqli::escape_string ( string $escapestr ) : string
mysqli::real_query ( string $query ) : bool
public mysqli::refresh ( int $options ) : bool
public mysqli::release_savepoint ( string $name ) : bool
mysqli::rollback ([ int $flags = 0 [, string $name ]] ) : bool
mysqli::rpl_query_type ( string $query ) : int
public mysqli::savepoint ( string $name ) : bool
mysqli::select_db ( string $dbname ) : bool
mysqli::send_query ( string $query ) : bool
mysqli::set_charset ( string $charset ) : bool
mysqli_set_local_infile_default ( mysqli $link ) : void
mysqli::set_local_infile_handler ( mysqli $link , callable $read_func ) : bool
mysqli_sqlstate ( mysqli $link ) : string
mysqli::ssl_set ( string $key , string $cert , string $ca , string $capath , string $cipher ) : bool
mysqli::stat ( void ) : string
mysqli::store_result ([ int $option ] ) : mysqli_result
mysqli_thread_id ( mysqli $link ) : int
mysqli_thread_safe ( void ) : bool
mysqli_warning_count ( mysqli $link ) : int
}

Índice

add a note add a note

User Contributed Notes 2 notes

up
2
user at note dot com
1 month 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
0
user at note dot com
1 month 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