PHP code example of webrium / foxql
1. Go to this page and download the library: Download webrium/foxql library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
webrium / foxql example snippets
use Foxdb\DB;
use Foxdb\Config;
DB::addConnection('main', [
'host'=>'localhost',
'port'=>'3306',
'database'=>'test',
'username'=>'root',
'password'=>'1234',
'charset'=>Config::UTF8,
'collation'=>Config::UTF8_GENERAL_CI,
'fetch'=>Config::FETCH_CLASS
]);
use Foxdb\DB;
$users = DB::table('users')->get();
foreach ($users as $user) {
echo $user->name;
}
$user = DB::table('users')->where('name', 'Jack')->first();
return $user->email;
$email = DB::table('users')->where('name', 'John')->value('email');
$user = DB::table('users')->find(3);
$user = User::find(3);
if($user){
$user->name = 'Tom';
$user->save(); // update name
}
$user = User::where('phone', '09999999999')->find();
if($user){
$user->phone = '09999999998';
$user->save(); // update user phone number
}
use Foxdb\DB;
$titles = DB::table('users')->pluck('title');
foreach ($titles as $title) {
echo $title;
}
$titles = DB::table('users')->pluck('title', 'name');
foreach ($titles as $name => $title) {
echo $title;
}
use Foxdb\DB;
DB::table('users')->orderBy('id')->chunk(100, function ($users) {
foreach ($users as $user) {
//
}
});
DB::table('users')->orderBy('id')->chunk(100, function ($users) {
// Process the records...
return false;
});
use Foxdb\DB;
DB::table('users')->orderBy('id')->each(function ($user) {
//
});
$page = 1;
$list = DB::table('posts')
->is('active')
->paginate(10, $page);
$list->total; // The total number of rows
$list->count; // The number of rows received on the current page
$list->per_page; // The number of rows to display on each page
$list->prev_page; // Previous page number. If not available, its value is false
$list->next_page; // next page number. If not available, its value is false
$list->current_page; // Current page number
$list->data; // List of data rows
use Foxdb\DB;
$users = DB::table('users')->count();
$price = DB::table('orders')->max('price');
$price = DB::table('orders')
->where('finalized', 1)
->avg('price');
if (DB::table('orders')->where('finalized', 1)->exists()) {
// ...
}
if (DB::table('orders')->where('finalized', 1)->doesntExist()) {
// ...
}
use Foxdb\DB;
$users = DB::table('users')
->select('name', 'email as user_email')
->get();
// Or you can send as an array
$users = DB::table('users')
->select(['name', 'email as user_email'])
->get();
$users = DB::table('users')
->select(function($query){
$query->field('name');
$query->field('email')->as('user_email');
})
->get();
$users = DB::table('users')
->select(DB::raw('count(*) as user_count, status'))
->where('status', '<>', 1)
->groupBy('status')
->get();
$users = DB::table('users')
->select(function($query){
$query->count('*')->as('user_count')
$query->field('status');
})
->get();
$orders = DB::table('orders')
->whereRaw('price > IF(state = "TX", ?, 100)', [200])
->get();
$orders = DB::table('orders')
->select('department', DB::raw('SUM(price) as total_sales'))
->groupBy('department')
->havingRaw('SUM(price) > ?', [2500])
->get();
use Foxdb\DB;
$users = DB::table('users')
->join('contacts', 'users.id', '=', 'contacts.user_id')
->join('orders', 'users.id', '=', 'orders.user_id')
->select('users.*', 'contacts.phone', 'orders.price')
->get();
$users = DB::table('users')
->select('users.*', 'orders.price')
->join('orders.user_id', 'users.id')
->get();
$users = DB::table('users')
->leftJoin('posts', 'users.id', '=', 'posts.user_id')
->get();
$users = DB::table('users')
->rightJoin('posts', 'users.id', '=', 'posts.user_id')
->get();
$sizes = DB::table('sizes')
->crossJoin('colors')
->get();
$users = DB::table('users')
->where('votes', '=', 100)
->where('age', '>', 35)
->get();
$users = DB::table('users')->where('votes', 100)->get();
$users = DB::table('users')
->where('votes', '>=', 100)
->get();
$users = DB::table('users')
->where('votes', '<>', 100)
->get();
$users = DB::table('users')
->where('name', 'like', 'T%')
->get();
$users = DB::table('users')
->where('votes', '>', 100)
->orWhere('name', 'John')
->get();
$users = DB::table('users')
->where('votes', '>', 100)
->orWhere(function($query) {
$query->where('name', 'Abigail')
->where('votes', '>', 50);
})
->get();
$products = DB::table('products')
->whereNot(function ($query) {
$query->where('clearance', true)
->orWhere('price', '<', 10);
})
->get();
$users = DB::table('users')
->whereBetween('votes', [1, 100])
->get();
$users = DB::table('users')
->whereNotBetween('votes', [1, 100])
->get();
$users = DB::table('users')
->whereIn('id', [1, 2, 3])
->get();
$users = DB::table('users')
->whereNotIn('id', [1, 2, 3])
->get();
$users = DB::table('users')
->whereNull('updated_at')
->get();
$users = DB::table('users')
->whereNotNull('updated_at')
->get();
$users = DB::table('users')
->whereDate('created_at', '2016-12-31')
->get();
$users = DB::table('users')
->whereMonth('created_at', '12')
->get();
$users = DB::table('users')
->whereDay('created_at', '31')
->get();
$users = DB::table('users')
->whereYear('created_at', '2016')
->get();
$users = DB::table('users')
->whereTime('created_at', '=', '11:20:45')
->get();
$users = DB::table('users')
->whereColumn('first_name', 'last_name')
->get();
$users = DB::table('users')
->whereColumn('updated_at', '>', 'created_at')
->get();
$users = DB::table('users')
->orderBy('name', 'desc')
->get();
$users = DB::table('users')
->orderBy('name', 'desc')
->orderBy('email', 'asc')
->get();
$user = DB::table('users')
->latest()
->first();
$randomUser = DB::table('users')
->inRandomOrder()
->first();
$users = DB::table('users')
->groupBy('account_id')
->having('account_id', '>', 100)
->get();
$users = DB::table('users')
->groupBy('first_name', 'status')
->having('account_id', '>', 100)
->get();
$users = DB::table('users')->skip(10)->take(5)->get();
$users = DB::table('users')
->offset(10)
->limit(5)
->get();
DB::table('users')->insert([
'email' => '[email protected] ',
'votes' => 0
]);
$id = DB::table('users')->insertGetId(
['email' => '[email protected] ', 'votes' => 0]
);
$affected = DB::table('users')
->where('id', 1)
->update(['votes' => 1]);
DB::table('users')->increment('votes');
DB::table('users')->increment('votes', 5);
DB::table('users')->decrement('votes');
DB::table('users')->decrement('votes', 5);
DB::table('users')->where('id', $id)->delete();
$active_list = DB::table('users')->is('active')->get();
// OR
$active_list = DB::table('users')->true('active')->get();
$inactive_list = DB::table('users')->is('active', false)->get();
//OR
$inactive_list = DB::table('users')->false('active')->get();
DB::table('users')
->is('active')
->and('credit', '>', 0)
->or('vip', true)
->get();
DB::table('users')
->in('id', [1,5,10])
->get();