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 Web Developers

Working Hard But Still Not Getting Ahead?

You finish a project, get paid, and then it's back to finding the next client.

Month after month, the cycle repeats.

That's why many web developers never build real financial freedom—even though they're highly skilled.

The developers creating long-term wealth are using those same skills to build SaaS products, plugins, and digital tools that generate recurring income.

What if your next project could pay you more than once?

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

Top Posts Viewed

Easily Edit HTML Table Rows Or Cells With jQuery
145 views
How To Create A Secure Login System With PHP And MySQL
133 views
Add Google Sign-In in 2 Minutes
111 views

Categories

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