Skip to content

mhromov/dbconnect

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 

Repository files navigation

dbconnect

Simple class to work with mysql.

Example : Initializing:

'localhost', 'username' => 'username', 'password' => 'pass1234', 'db_name' => 'maindb' ]; $db = new DbConnect($configs); ?>
  1. Make a query:
query('CREATE TABLE users(id int,name varchar(255),surname varchar(255),email varchar(255),friends_count int);'); /* $result = ['success' => 1] if there is no error $result = ['success' => 0, 'error'=>'Some mysql error'] if there is some mysql error */ ?>
  1. Select
get( 'users', // Table name "name='Bob'", // Where string (without word 'Where') 10, // Limit 'id DESC', // Order ['name', 'surname'] // Fields to select ); /* Example output of $users: [ [ 'id'=>1, 'name'=>'Bob', 'email'=>'' ], [ 'id'=>2, 'name'=>'Bob', 'email'=>'' ] ] OR ['success' => 0, 'error'=>'Some mysql error'] if there is some mysql error OR [] if the table is empty */ foreach ($user in $users) { echo $user['name'].' '.$user['surname']; echo '
'; } ?>
  1. Insert
insert('users', ['name'=>'Alice', 'surname'=>'Smith']); // Table name and array of insert params ?>
  1. Update
update->('users', ['name'=>'NotBob']); // Set all user's names to 'NotBob' $db->update->('users', ['name'=>'NotBob'], 'id=1', 10); // Set all user's names with id=1 with limit 10 ?>
  1. PlusOne (if you want to just insrement some parameter)
plusOne('users', 'friends_count') // +1 to friends_count of all users $db->plusOne('users', 'friends_count', 'id=1', 10) // +1 to friends_count of users with id=1 with limit 10 $db->plusOne('users', 'friends_count', 'id=1', 10, true) // -1 to friends_count of users with id=1 with limit 10 ?>
  1. Delete
remove('users', 'id=1'); $db->remove('users', 'name="Bob"', 10); // With limit 10 ?>
  1. Prepare string to use in WHERE string
stringPrep('"name with" some bad symbols "'."'"); $db->get('users', "name='$name'", 10); ?>

About

My mysql-orm

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages