1. Go to this page and download the library: Download lianzh/sql 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/ */
lianzh / sql example snippets
\LianzhSQL\Sql::ds # get data source object
\LianzhSQL\Sql::assistant # Sql auxiliary class object obtained
use LianzhSQL\SqlHelper;
function assertEqual($var1,$var2){
if ($var1 !== $var2)
throw new Exception('Not Equal .');
}
$ds = JptestApp::$ds;
/* @var $ds SqlDataSource */
$cond = "author_id=123 AND bookname='仓一鼠'";
$result = SqlHelper::parse_cond($ds,$cond,FALSE);
assertEqual($result,"author_id=123 AND bookname='仓一鼠'");
// ? Is an array
$cond = array(
'author_id' => 123,
'bookname' => '仓一鼠',
);
$result = SqlHelper::parse_cond($ds,$cond,FALSE);
assertEqual($result,"author_id = 123 AND bookname = '仓一鼠'");
// > < !=
$cond = array(
'author_id' => array(123, '>'),
'bookname' => '仓一鼠',
);
$result = SqlHelper::parse_cond($ds,$cond,FALSE);
assertEqual($result,"author_id > 123 AND bookname = '仓一鼠'");
$cond = array(
'author_id' => array(123, '<'),
'bookname' => '仓一鼠',
);
$result = SqlHelper::parse_cond($ds,$cond,FALSE);
assertEqual($result,"author_id < 123 AND bookname = '仓一鼠'");
$cond = array(
'author_id' => array(123, '!='),
'bookname' => '仓一鼠',
);
$result = SqlHelper::parse_cond($ds,$cond,FALSE);
assertEqual($result,"author_id != 123 AND bookname = '仓一鼠'");
// fuzzy query
$cond = array(
'bookname' => array('%仓一鼠%','LIKE'),
);
$result = SqlHelper::parse_cond($ds,$cond,FALSE);
assertEqual($result,"bookname LIKE '%仓一鼠%'");
// 'IN','NOT IN'
$cond = array(
'author_id' => array( array(123,124,125) ),
);
$result = SqlHelper::parse_cond($ds,$cond,FALSE);
assertEqual($result,"author_id IN (123,124,125)");
$cond = array(
'author_id' => array( array(123,124,125), 'IN'),
);
$result = SqlHelper::parse_cond($ds,$cond,FALSE);
assertEqual($result,"author_id IN (123,124,125)");
$cond = array(
'author_id' => array( array(123,124,125), 'NOT IN'),
);
$result = SqlHelper::parse_cond($ds,$cond,FALSE);
assertEqual($result,"author_id NOT IN (123,124,125)");
// BETWEEN AND , NOT BETWEEN AND
$cond = array(
'author_id' => array( array(10,25), 'BETWEEN_AND'),
);
$result = SqlHelper::parse_cond($ds,$cond,FALSE);
assertEqual($result,"author_id BETWEEN 10 AND 25");
$cond = array(
'author_id' => array( array(10,25), 'NOT_BETWEEN_AND'),
);
$result = SqlHelper::parse_cond($ds,$cond,FALSE);
assertEqual($result,"author_id NOT BETWEEN 10 AND 25");
// author_id > 15 OR author_id < 5 AND author_id != 32
$cond = array(
'author_id' => array(
array( array(15,'>','OR'),array(5,'<','AND'), array(32,'!=') ) ,
'FIELD_GROUP'
),
);
$result = SqlHelper::parse_cond($ds,$cond,FALSE);
assertEqual($result," (author_id > 15 OR author_id < 5 AND author_id != 32)");
// OR AND connector
$cond = array(
'author_id' => array(123, '!=' ,'AND'),
'bookname' => array('仓一鼠', '=' ,'OR'),
'book_price' => array(45, '<=' ,'AND'),
);
$result = SqlHelper::parse_cond($ds,$cond,FALSE);
assertEqual($result,"author_id != 123 AND bookname = '仓一鼠' OR book_price <= 45");
// Special characters in the value of the incoming condition will be automatically escaped by qstr
$cond = array(
'bookname' => array("%色'色%",'LIKE'),
);
$result = SqlHelper::parse_cond($ds,$cond,FALSE);
// note osc 'There is a problem with symbol resolution, so 2 \
assertEqual($result,"bookname LIKE '%色\\'色%'");
// 数据表字段名比较
$cond = array(
'author_id' => array(123, '!=' ,'AND'),
'book_price' => array("market_parce",'>','AND',true),
);
$result = SqlHelper::parse_cond($ds,$cond,FALSE);
assertEqual($result,"author_id != 123 AND book_price > market_parce");
Description of configuration information
1. type = mysql/mariadb
{
dbpath: mysql:host=${host};port=${port};dbname=${database}
initcmd: [
SET NAMES '${charset}',
]
}
2. type = pgsql
{
dbpath: pgsql:host=${host};port=${port};dbname=${database}
initcmd: [
SET NAMES '${charset}',
]
}
3. type = sybase
{
dbpath: sybase:host=${host};port=${port};dbname=${database}
initcmd: [
SET NAMES '${charset}',
]
}
4. type = sqlite
{
dbpath: sqlite:${file}
initcmd: [
]
}
5. type = mssql
{
Windows:
dbpath: sqlsrv:server=${host};port=${port};database=${database}
Linux:
dbpath: dblib:host=${host};port=${port};dbname=${database}
initcmd: [
SET QUOTED_IDENTIFIER ON,
SET NAMES '${charset}',
]
}
If you want to use persistent connections, you can configure attr parameters
attr: [
PDO::ATTR_PERSISTENT => TRUE,
]
The PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC used by the class cannot be changed
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.