Download the PHP package sorskod/db without Composer

On this page you can find all versions of the php package sorskod/db. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package db

PDO wrapper with query builder

PDO wrapper extends PDO and PDOStatement classes and add some nice methods as insert/update/delete and so on. Also, there is very useful SQL query builder.

API

Because library extends PDO driver, you can use all of native PDO methods and new additional:

DB - The database class

Statement

Query - Build SQL query object

See more information about how to use database query builder.

Usage examples

Creating database instance

$db = new database\DB("mysql:host=localhost;dbname=YOUR_DB_NAME", "YOUR_DB_USERNAME", "YOUR_DB_PASSWORD");

Select

Execute query and fetch User object:

class User {}

$user_id = 1;
$sql = "SELECT * FROM users WHERE user_id = ? AND is_active = ?";
$user = $db->executeQuery($sql, array($user_id, 1))
    ->fetchInto(new User); // or ->fetchObject("User") as in standard PDO driver

If you need a collection of User objects, you can use fetchCollection method:

$users = $db->executeQuery($sql, array($user_id, 1))
    ->fetchCollection(new User); // or ->fetchCollection("User");

More complex, with query builder. You can build 'native' structure of objects. For example, you can fetch collection of objects Post and every Post object may have a property $author which is a instance of User object:

class User
{
    /**
     * Get user's first and last name
     *
     * @return string
     */
    function getName() {
        return $this->first_name . " ". $this->last_name;
    }
}

class Post
{
    /**
     * @var User
     */
    public $author;
}

// Library need FETCH_TABLE_NAMES option for mapping class names and table names
$db->setFetchTableNames(1);

$sql = $db->select("p.*, u.*")
    ->from("posts p")
    ->join("INNER JOIN users u USING(user_id)")
    ->where("u.user_id = ?", $user_id)
    ->orderBy("p.title");

$stmt = $sql->execute();

/* @var Post[] $post_collection  */
$post_collection = array();

// Fetching data into Post object from posts table (p is alias)
while($post = $stmt->fetchInto(new Post, "p")) {

    // fetch User object from users table (u is alias)
    $post->author = $stmt->fetchIntoFromLastRow(new User, "u");

    $post_collection[] = $post;
}

// You can send $post_collection from model to view in your controller, so here is usage in view
foreach($post_collection as $post) {
    echo $post->author->getName();
}

Insert

Library has insert method for easy inserting array or object as row to database table. Note that all other properties or elements that not match column names will be ignored.

$data = array(
    "username" => "User 1234",
    "email" => "[email protected]",
    "mtime" => time()
);
$db->insert("users", $data);

Insert with prepared statement

Third param for insert() method is "unique prepared stmt key". Every insert which have that key will use the same prepared statement.

foreach($data_array as $data) {
    $db->insert("users", $data, "unique_stmt_key");
}

Update

Some examples of update statement

$user_id = 1;
$db->update("users", $data, "user_id = ?", $user_id);
$db->update("users", $data, "user_id = ? AND email = ?", array(1, "[email protected]"));

Saving data

Automatic determination of INSERT or UPDATE. If $data['user_id'] exits it will be UPDATE, otherwise it will be INSERT.

$db->save("users", $data, "user_id"); // user_id is name of PRIMARY column

More examples

// Delete row in table
// some as $db->exec("DELETE FROM users WHERE user_id = 1");
$db->delete("users", "user_id = ?", $user_id);

// Count rows in table
$count = $db->count("users");

/* @var User[] $users Collection of User objects */
$users = $db->executeQuery("SELECT * FROM users")->fetchCollection(new User);

See more examples for Sakila database


All versions of db with dependencies

PHP Build Version
Package Version
Requires php Version >=5.3.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package sorskod/db contains the following files

Loading the files please wait ....