PHP code example of phpple / mysql

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

    

phpple / mysql example snippets


use Phpple\Mysql\Conf;
use Phpple\Mysql\Sql\SqlBuilder;
use Phpple\Mysql\Db;

// 初始化数据库配置
$confs = [
    'db' => [
        'demo' => [
            'dbname' => 'phpple',
            'instance' => 'ip1'
        ],
    ],
    'instance' => [
        'ip1' => [
            'host' => '127.0.0.1',
            'port' => 3306,
            'user' => 'root',
            'pass' => '',
            'charset' => 'utf8'
        ],
    ]
];
Conf::init($confs);

$id = 12030;
// 创建一个Sql构建器
$sqlBuilder = SqlBuilder::withTable('u_user')
    ->fields('view_num')
    ->setData([
        '@view_num' => '(view_num+1)'
    ])
    ->where('id', $id);
// 绑定Sql构建器到Db对象
$db = Db::get('demo')->sqlBuilder($sqlBuilder);

// 获取原始view_num
$viewNum = $db->getSingle();
echo 'before:' . $viewNum . PHP_EOL;

// view_num 自增1
$db->update();

// 重新获取view_num
$newViewnum = $db->getSingle();
echo 'after:' . $newViewnum . PHP_EOL;