Page tree
Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

Description


Core\Db db()


Access our database layer with the db() function. It supports method chaining.

Examples


<?php

// Run a query to get at least 10 users
$users = db()->select('*')
   ->from(':user')
   ->order('user_id ASC')
   ->limit(10)
   ->all();
 
// Get a single user
$user = db()->select('*')->from(':user')->where(['user_id' => 1])->get();
 
// Insert an entry into the DB
$insert_id = db()->insert(':user', ['user_name' => 'Foo']);
 
// Update an entry in the DB
db()->update(':user', ['user_name' => 'bar'], ['user_id' => 1]);
 
// Get the total rows
$total = db()->select('COUNT(*)')->from(':user')->count();
 
// Joining tables
db()->select('f.*, b.*')
   ->from(':foo', 'f')
   ->join(':bar', 'b', 'b.id = f.id')
   ->get();

Methods


MethodDescriptionUsageReturns

select(string $select)

 


$select

SQL Select

Select statement for sql query

// Method chaining
->select('user_id, user_name')
 
// SQL equivalent
SELECT user_id, user_name

object Core\Db

from(string $table [, string $alias] )

 


$table

Database table name.


$alias

(optional) Table alias.

 Define the table in your SELECT query.

Table Prefix: Be sure to prefix tables with a colon.

 // Method chaining
->from(':user', 'u')

// SQL equivalent
FROM prefix_user AS u
object Core\Db
  • No labels