Skip to content

pourfallah/simple-php-mysql

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 

Repository files navigation

simple-php-mysql

Simple Mysql Class in PHP

Simple PHP MySQL is a lightweight PHP class designed to simplify interactions with MySQL databases using MySQLi. It provides methods for connecting, querying, inserting, updating, deleting, and counting records with ease and security.

Table of Contents

Features

  • Easy Database Connection: Simplifies establishing a connection to your MySQL database.
  • CRUD Operations: Methods to Create, Read, Update, and Delete records effortlessly.
  • Secure Queries: Utilizes real escaping to protect against SQL injection.
  • Error Handling: Comprehensive error logging and reporting.
  • Flexible Usage: Suitable for small to medium-sized projects.

Installation

You can include the MySQL.php class in your project by following these steps:

Using Composer (Recommended)

  1. Initialize Composer (if you haven't already):
    composer init
    
  2. Require the Package:
    composer require pourfallah/simple-php-mysql
    
  3. Include Composer’s Autoload in Your PHP Script:
    require 'vendor/autoload.php';

Manual Installation

  1. Download the MySQL.php File: Clone the repository or download the file directly from GitHub.

  2. Include the Class in Your PHP Script:

    require_once 'path/to/MySQL.php';

Usage Example:

Here’s a basic example of how to use the MySQL class in your project:

<?php
// Include the MySQL class (assuming it's saved as MySQL.php)
require_once 'MySQL.php';

// Create a new MySQL instance
$db = new MySQL();

// Connect to the database
if ($db->connect('username', 'password', 'database_name', 'localhost')) {
    // Insert a new record
    $insertId = $db->insert('users', [
        'username' => 'john_doe',
        'email' => 'john@example.com',
        'age' => 30
    ]);

    if ($insertId) {
        echo "New user inserted with ID: " . $insertId;
    }

    // Fetch a single user
    $user = $db->line("SELECT * FROM `users` WHERE `id` = $insertId");
    print_r($user);

    // Update the user's age
    $db->update('users', ['age' => 31], "id = $insertId");

    // Count the number of users
    $userCount = $db->countRecords('users');
    echo "Total users: " . $userCount;

    // Delete the user
    $db->delete('users', "id = $insertId");

    // Close the connection
    $db->close();
} else {
    // Handle connection error
    print_r($db->error);
}
?>

Parameters

  • connect($user, $pass, $databasename, $host, $port): Establishes a connection to the MySQL database.
  • insert($table, $data, $where, $options): Inserts a new record into a specified table.
  • line($Query): Executes a query and returns a single row.
  • table($Query,$id, $unic): Executes a query and returns multiple rows.
  • update($table, $data, $condition, $limit): Updates existing records in a table.
  • delete($table, $condition): Deletes records from a table.
  • countRecords($table, $condition): Counts the number of records in a table based on a condition.
  • lastId(): Retrieves the ID of the last inserted record.
  • close(): Closes the database connection.

License

This project is licensed under the MIT License. You are free to use, modify, and distribute this software as per the terms of the license.

Author

Acknowledgments

  • Inspired by the need for simple and secure PHP-MySQL interactions.
  • Thanks to the open-source community for their invaluable resources and support.

About

Simple Mysql Class in PHP

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages