Code With Mark
Home
About
Resources
Contact

PHP Simple Database Class

Simple PHP MySQL database class for secure CRUD operations. Easily create, read, update, and delete records with minimal code.

If you are looking for a simple way to connect to your MySQL database using PHP and perform CRUD operations (Create, Read, Update, Delete), then PHP Simple Database Class is the right tool for you.

With this class, you can quickly build secure and powerful web applications using minimal code.

Download Functions


Add Database Connection Settings

At the top of your PHP page (AJAX or include file), add:

$db_conn = array(
    'host' => 'localhost',
    'user' => 'root',
    'pass' => '',
    'database' => 'test',
);

$db = new SimpleDBClass($db_conn);

Warning Settings

Inside the class file, you will notice:

// To show query error messages set on
// To hide them set off

public $ShowQryErrors = 'on';

During development and testing, keep this setting turned on so you can identify errors quickly.

Once your application is ready for production, switch it to off.


Functions You Need To Know

There are 5 main query functions:

  • Select → Get rows
  • Insert → Add rows
  • Update → Update rows
  • Delete → Delete rows
  • Qry → General purpose query

There is also 1 important security function:

  • CleanDBData()

It is highly recommended that you sanitize all user data before inserting it into your database.


Select Function

Retrieve rows from your database table.

$db->select("select * from users");

This returns all rows from the users table.

Select Rows Using Criteria

$user_id = $db->CleanDBData($_POST['user_id']);

$db->select("
    select * from users
    where user_id='$user_id'
");

Insert Function

$insert_arrays = array(
    'user_id' => $db->CleanDBData($UserID),
    'user_email' => $db->CleanDBData($UserEmail),
    'user_password' => $db->CleanDBData($UserPassword),
);

$db->Insert('users', $insert_arrays);

Always hash your passwords before storing them into your database.


Update Function

$array_fields = array(
    'userid' => $db->CleanDBData($UserID),
    'sitename' => $db->CleanDBData($UserSite),
);

$array_where = array(
    'rec_id' => $RecID,
);

$db->Update(
    $strTableName,
    $array_fields,
    $array_where
);

Delete Function

$array_where = array(
    'site_name' => $site_name,
    'user_email' => $user_email,
);

$Qry = $db->Delete(
    $tabl_name,
    $array_where
);

Final Thoughts

This PHP Simple Database Class was built to help developers create secure and powerful applications faster without dealing with unnecessary complexity.

If you want a lightweight way to handle MySQL CRUD operations using PHP, this class can save you a lot of development time.

For PHP Developers

Your Next PHP Project Could Make You Money for Years

Most developers get paid once for the code they write. But the developers building real wealth use those same skills to create products that generate income over and over again.

A simple SaaS, plugin, web app, or digital product can continue bringing in customers long after it's launched.

Learn How Developers Build Monthly Income →
GOOGLE SHEETS HACK That Creates Files AutomaticallyGOOGLE SHEETS HACK That Creates Files Automatically←Previous

Top Posts Viewed

Using JavaScript Window Onload Event Correctly
422 views
Learn To Create YouTube Video Downloader
398 views
How Google Developers Think (And Why You Should Too)
383 views

Categories

Courses
Excel
Google Script
Javascript
jQuery
Microsoft Access
MongoDB
Node JS
PHP
Quick Tip
Uncategorized
Wordpress