PHP code example of websitesql / database

1. Go to this page and download the library: Download websitesql/database 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/ */

    

websitesql / database example snippets


// Initialize the database provider
$db = new WebsiteSQL\Database\Database([
    'type' => 'mysql',
    'host' => 'localhost',
    'database' => 'name',
    'username' => 'your_username',
    'password' => 'your_password'
], '../migrations');

// Select all records from users table
$users = $db->select("users", "*");

// Select with conditions
$user = $db->get("users", "*", ["id" => 1]);

// Select with JOIN
$data = $db->select("posts", [
    "[>]users" => ["user_id" => "id"]
], [
    "posts.id",
    "posts.title",
    "users.username"
], [
    "posts.status" => "published",
    "ORDER" => ["posts.created" => "DESC"]
]);

$db->insert("users", [
    "username" => "john_doe",
    "email" => "[email protected]",
    "created" => date("Y-m-d H:i:s")
]);

// Get last inserted ID
$lastId = $db->id();

$db->update("users", [
    "email" => "[email protected]"
], [
    "id" => 1
]);

$db->delete("users", [
    "id" => 1
]);

// Run migrations
$db->migrations()->up();

// Rollback the last batch of migrations
$db->migrations()->down();

// Rollback all migrations
$db->migrations()->reset();

// Refresh migrations (rollback all and run again)
$db->migrations()->refresh();