Skip to content

webdeasy/php-mysql-database-class

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 

Repository files navigation

PHP Database Class

This is a simple PHP class to connect to a MySQL database and execute queries. It is a very simple class, but it is very useful for smaller projects.

Requirements

  • PHP > 7.0 needs to be installed on your server

Basic Usage

To use the class, you first have to include the class file in your PHP file. You can do this by using the following code:

require_once("Database.php");

After you have included the class file, you can create a new instance of the class. You can do this by using the following code:

$db = new Database("localhost", "your-username", "your-password", "your-database");

Now you can execute queries. You can do this by using the following code:

$result = $db->query("SELECT * FROM users WHERE id = ?", array(1));

while($row = mysqli_fetch_assoc($result)) {
  print_r($row);
}

The query() function returns a mysqli_result object. You can use all functions like mysqli_fetch_assoc() on this object.

Functions

Function Description Parameters Return Value
open() Opens a connection to the database
close() Closes the connection to the database
query() Executes a query query (string), params (array) mysqli_result object
get_affected_rows() Returns the number of affected rows
(same as affected_rows and num_rows)
int
get_last_inserted_id() Returns the last inserted ID int

Examples and Details

Find a detailed documentation and some examples on my blog: https://webdeasy.de/en/php-mysql-database-class

About

This is a simple PHP class to connect to a MySQL database and execute queries.

Topics

Resources

Stars

Watchers

Forks

Languages