About Us
Team
Values
Support
Open Source: dbentity
Overview
Download
Presentations and Articles
Examples: DBEntity Calling
Examples: DBEntity Classes
Examples: DBEntity Syntax
Examples: DB
Examples: DBQUERY
Examples: RBA
FAQ
Contact
ABOUT US
DBEntity - DB - Examples

// echo_r(...) just prints in a structured format.

$qs = array('drop table if exists categories'
            , 'create table categories (id int primary key auto_increment, name varchar(32))'
            , 'drop table if exists products'
            , 'create table products (id int primary key auto_increment, category_id int, name varchar(32))'
            );
foreach ($qs as $q) { db::q($q);}
list ($category_id) = mg('categories')->create_ex('name', 'tools');

// db::insert
$product_a_id = db::insert('products', array('name' => 'widget a', 'category_id' => $category_id));
$product_b_id = db::insert('products', array('name' => 'widget b', 'category_id' => $category_id));

// db::object(s)
echo_r(db::objects('select * from products'));
echo_r(db::objects('products', compact('category_id')));
echo_r(db::objects('select * from products p inner join categories c on c.id = p.category_id'));
echo_r(db::object('products', $product_a_id));
echo_r(db::object('select * from products p inner join categories c on c.id = p.category_id limit 1'));

// db::update
db::update('products', array('name' => 'widget b changed'), $product_b_id);

// db::assocs
echo_r(db::assocs('select * from products'));
echo_r(db::assocs('products', compact('category_id')));
echo_r(db::assocs('select * from products p inner join categories c on c.id = p.category_id'));
echo_r(db::assoc('products', $product_a_id));
echo_r(db::assoc('select * from products p inner join categories c on c.id = p.category_id limit 1'));

// db::string(s)
echo_r(db::string('select name from products limit 1'));
echo_r(db::strings('select name from products'));

// db::kvdict
echo_r(db::kvdict('select id,name from products'));