PHP code example of shiwolang / db

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

    

shiwolang / db example snippets


DB::init([
    'database_type' => 'mysql',
    'database_name' => 'dbname',
    'server'        => 'localhost',
    'username'      => 'username',
    'password'      => 'yourpass',
    'charset'       => 'utf8'
]);

DB::init([
    'database_type' => 'mysql',
    'database_name' => 'dbname',
    'server'        => 'localhost',
    'username'      => 'username',
    'password'      => 'yourpass',
    'charset'       => 'utf8'
], "connection1");

DB::init($PDO);

DB::init($PDO, "connection1");

DB::connection();

DB::connection("connection1");

$lastInsertId = DB::connection()->insert("content", [
    "title"       => "title1",
    "content"     => "content1",
    "time"        =>  time()
]);

$lastInsertId = DB::connection()->delete("content", "id = :id", [":id" => 1]);

$lastInsertId = DB::connection()->update("content", [
    "title"       => "title1",
    "content"     => "content1",
    "time"        =>  time()
],"id = :id", [":id" => 1]);

DB::connection()->query("SELECT * FROM content where title = 'title1' LIMIT 10")->all();

DB::connection()->query("SELECT * FROM content WHERE title = :title LIMIT :limit", [
    ":title"     => "title1",
    ":limit"     =>  10
])->all();

DB::connection()->query("SELECT * FROM content WHERE id = ? LIMIT ?", ["title1", 10])->all();

DB::connection()->query("SELECT * FROM content where title = 'title1' LIMIT 10")->all();

DB::connection()->query("SELECT * FROM content where title = 'title1' LIMIT 10")->bindToClass(Content::class)->all();
DB::connection()->query("SELECT * FROM content where title = 'title1' LIMIT 10")->all(Content::class);

DB::connection()->query("SELECT * FROM content where title = 'title1' LIMIT 10")->all(function($title, $content, $time){
    return [
        "title"     => $title,
        "content"   => $content,
        "time"      => date("Y-m-d H:i:s", $time)
    ];
});

DB::connection()->query("SELECT * FROM content LIMIT 10")->each(function ($row) {
    print_r($row);
});

DB::connection()->query("SELECT * FROM content LIMIT 10")->each(function ($row) {
    print_r($row);
}, Content::class);

DB::connection()->query("SELECT * FROM content LIMIT 10")->json();

DB::connection()->query("SELECT * FROM content LIMIT 10")->json($data, Content::className());

$db = DB::connection();
$db->beginTransaction();
try {
    $lastInsertId = $db->insert("content", [
        "title"       => "title1",
        "content"     => "content1",
        "time"        =>  time()
    ]);
    $db->commit();
} catch (\Exception $e) {
    $db->rollBack();
    throw $e;
}

$db = DB::connection();
$db->beginTransaction();
try {

    try {
        $lastInsertId = $db->insert("content", [
            "title"       => "title1",
            "content"     => "content1",
            "time"        =>  time()
        ]);
        
        $db->commit();
    } catch (\Exception $e) {
        $db->rollBack();
        throw $e;
    }
    
    $lastInsertId = $db->insert("content", [
        "title"       => "title2",
        "content"     => "content2",
        "time"        =>  time()
    ]);
    
    $db->beginTransaction();
    try {
        $lastInsertId = $db->insert("content", [
            "title"       => "title3",
            "content"     => "content3",
            "time"        =>  time()
        ]);
        $db->commit();
    } catch (\Exception $e) {
        $db->rollBack();
        throw $e;
    }
    
    $db->commit();
} catch (\Exception $e) {
    $db->rollBack();
    throw $e;
}

DB::connection()->transaction(function () {
        DB::connection()->transaction(function () {
                DB::connection()->insert("content", [
                    "title"       => "title1",
                    "content"     => "content1",
                    "time"        =>  time()
                ]);
            }
        });
        
        DB::connection()->insert("content", [
            "title"       => "title2",
            "content"     => "content2",
            "time"        =>  time()
        ]);
        
        DB::connection()->transaction(function () {
                DB::connection()->insert("content", [
                    "title"       => "title3",
                    "content"     => "content3",
                    "time"        =>  time()
                ]);
            }
        });
    }
});

DB::connection()->query("SELECT * FROM content LIMIT 1")->all();
DB::connection()->query("SELECT * FROM content WHERE title = :title LIMIT :limit", [
    ":title" => "title1",
    ":limit" => 10
])->all();;
print_r(DB::connection()->getLog());

$sql = DB::connection()->query("SELECT * FROM content WHERE title = :title LIMIT :limit", [
    ":title" => "title1",
    ":limit" => 10
], true);

class Content
{
    private $id;
    private $title;
    private $content;

    /**
     * @return mixed
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @param mixed $id
     */
    public function setId($id)
    {
        $this->id = $id;
    }

    /**
     * @return mixed
     */
    public function getTitle()
    {
        return $this->title;
    }

    /**
     * @param mixed $title
     */
    public function setTitle($title)
    {
        $this->title = $title;
    }

    /**
     * For json formater
     * @json
     * @return mixed
     */
    public function getContent()
    {
        return $this->content . "_nihao";
    }

    /**
     * @param mixed $content
     */
    public function setContent($content)
    {
        $this->content = $content;
    }
    
    public function __get($name)
    {
        $getter = 'get' . self::camelName($name);

        if (method_exists($this, $getter)) {
            return $this->$getter();
        }

        throw new \Exception('Getting unknown property: ' . get_class($this) . '::' . $name);
    }
    public function __set($name, $value)
    {
        $setter = 'set' . self::camelName($name);
        if (method_exists($this, $setter)) {
            $this->$setter($value);

            return;
        }

        throw new \Exception('Setting unknown property: ' . get_class($this) . '::' . $name);
    }
    protected static function camelName($name, $ucfirst = true)
    {
        if (strpos($name, "_") !== false) {
            $name = str_replace("_", " ", strtolower($name));
            $name = ucwords($name);
            $name = str_replace(" ", "", $name);
        }

        return $ucfirst ? ucfirst($name) : $name;
    }
}