medoo tutorial

Ultimate Tutorial Using Medoo PHP Framework

Posted on

Medoo is a PHP framework that interesting to me. Why? because this framework is very small, only a few kilobytes, and only 1 piece of file. I call Medoo is a real micro PHP framework. Medoo has primary responsibility only interact with the database, no helpers and controllers, so right if Medoo referred to as non-MVC framework.

With Medoo we can interact with databases, especially MySQL very easily and quickly, because in the framework Medoo there are many functions for it. Suppose the function select, insert, update and delete. Isn’t the basic operation of the database is CRUD? so, this framework already qualified. Instead you use PHP from scratch, it is better to use this framework.

[code tipe=”336″]

Medoo made ​​by Angel Lai, someone who likes things simple and easy.

This post about tutorial use Medoo, from starting to advance. We learn together

1. Setting up Your Medoo Framework

Please download a copy of Medoo, you can download it via this link (minify version, recomended for production) or this link (when development app). Store it on you local drive and you ready to follow this tutorial.

Setting up your MySQL database

Open medoo.php file, edit some code below, make sure you know your database configuration.

protected $database_type = 'mysql';
protected $server = 'localhost';
protected $username = 'your_username';
protected $password = 'your_password';

Please fill according to your database. I think you’ll have no trouble doing it. Ok, save and you’re done,  medoo connect with the database perfectly.

[code tipe=”300″]

2. Select Query

We could easily do the select query to a database via medoo framework. Sample code is as below

$database = new medoo();
 
$datas = $database->select("account", [
	"user_name",
	"email"
], [
	"user_id[>]" => 100
]);

Select all column in account table

$datas = $database->select("account", "*");

or select a column in account table

$datas = $database->select("account", "user_name");

and then print data from database in your browser

[code tipe=”728″]

foreach($datas as $data)
{
	echo "user_name:" . $data["user_name"] . " - email:" . $data["email"] . "<br/>";
}

Easy to do following this tutorial? yes, because medoo very easy to learn and use. Ok, now for the next tutorial that is, insert data into the database.

3. Insert Record to Database

$database = new medoo();
 
$last_user_id = $database->insert("account", [
	"user_name" => "foo",
	"email" => "[email protected]",
	"age" => 25
]);

Of course, we can insert multiple record in one action

$last_user_id = $database->insert("account", [
[
	"user_name" => "foo",
	"email" => "[email protected]",
	"age" => 25,
	"city" => "New York"
],
[
	"user_name" => "bar",
	"email" => "[email protected]",
	"age" => 14,
	"city" => "Hong Kong"
]);

So, we do not need a looping command.

4. Update Command

$database->update("account", [
 "name"=>"John Doe",
 "phone"=>"0984758"
  ],
  [
"user_id"=>10
]);

easy and simple lines of code, you can also play with it.

[code tipe=”link”]

5. Delete Command

$database = new medoo();
 
$database->delete("account", [
	"AND" => [
		"type" => "business"
		"age[<]" => 18
	]
]);

That’s a brief tutorial using Medoo framework. I will update this tutorial when finished with my affairs.

Incoming search terms:

  • medoo with codeigniter
  • simple insert update delete in codeigniter
Gravatar Image
Web developer, big fan of CakePHP, loving wordpress soo much. You can found him on Google+ and Facebook. Dont forget to follow him on Twitter.