165 行
5.4 KiB
PHP
165 行
5.4 KiB
PHP
<?php
|
|
require_once('../config.php');
|
|
|
|
/* val
|
|
*
|
|
* select => ['var1', 'var2'...]
|
|
* insert|update|where => ['key1' => 'value1', 'key2' => 'value2'...]
|
|
* order => ['var', 'asc'|'desc']
|
|
* limit => int
|
|
* */
|
|
function lolidb (string $table, array $val, string $mode='select'): array|bool {
|
|
// モードは違う場合、やめる。If the mode is wrong, stop.
|
|
if ($mode != 'select' && $mode != 'one' && $mode != 'insert' && $mode != 'update' && $mode != 'delete') {
|
|
dd("モードは「select」、「one」、「insert」、「update」、「delete」です。\nMode should be \"select\", \"one\", \"insert\", \"update\", or \"delete\".");
|
|
return false;
|
|
}
|
|
|
|
// 順のチェック。Check order.
|
|
if (isset($val['order'])) {
|
|
// selectかoneじゃない場合。If not select or one.
|
|
if ($mode !== 'select' || $mode !== 'one') {
|
|
dd("「order」は「select」又は「one」のみで使えます。\n\"order\" can only be used with \"select\" or \"one\".");
|
|
return false;
|
|
}
|
|
|
|
// 複数の場合。If multiple.
|
|
if (count($val['order'] > 1)) {
|
|
dd("複数「order」は未対応です。\nMultiple \"order\" is not yet supported.");
|
|
return false;
|
|
}
|
|
|
|
// ascかdescかどうか。Whether it's asc or desc.
|
|
foreach ($val['order'] as $k => $v) {
|
|
if ($v !== 'asc' && $v !== 'desc') {
|
|
dd("「order」は「asc」又は「desc」です。\n\"order\" should be \"asc\" or \"desc\".");
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
// 限界のチェック。Check limit.
|
|
if (($mode === 'select' || $mode === 'one') && isset($val['limit'])) {
|
|
// oneの場合。If one.
|
|
if ($mode === 'one') {
|
|
dd("「one」の場合、「limit」はいつでも「1」です。「limit」は1以上は必要の場合、「select」を使って下さい。\nIn case of \"one\", \"limit\" is always \"1\". If \"limit\" needs to be more than \"1\", please use \"select\".");
|
|
return false;
|
|
}
|
|
|
|
// selectじゃない場合。If not select.
|
|
else if ($mode !== 'select') {
|
|
dd("「insert」、「update」及び「delete」で、「limit」を使えません。\n\"limit\" cannot be used with \"insert\", \"update\", and \"delete\".");
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// insertの場合。In case of insert.
|
|
if ($mode === 'insert' && !isset($val['insert'])) {
|
|
dd("「insert」モードの場合、「insert」値は必須です。\nIn case of \"insert\" mode, \"insert\" value is necessary.");
|
|
return false;
|
|
}
|
|
|
|
// updateの場合。In case of update.
|
|
if ($mode === 'update' && !isset($val['update'])) {
|
|
dd("「update」モードの場合、「update」値は必須です。\nIn case of \"update\" mode, \"update\" value is necessary.");
|
|
return false;
|
|
}
|
|
|
|
// MySQLiの創作。Creation of MySQLi.
|
|
$mysqli = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_TABLE);
|
|
$result = null;
|
|
|
|
if ($mode == 'select' || $mode == 'one') {
|
|
// select値がなければ、全部を選択する。If there is no select value, select everything.
|
|
if (!isset($val['select'])) {
|
|
$val['select'] = ['*'];
|
|
}
|
|
|
|
$query = 'SELECT '.$val['select'].' FROM '.$table;
|
|
|
|
if (isset($val['where'])) {
|
|
$query .= ' WHERE ';
|
|
foreach ($val['where'] as $k => $v) {
|
|
$query .= $k.' = '.$v;
|
|
if ($k !== array_key_last($val['where'])) $query .= ' AND ';
|
|
}
|
|
}
|
|
|
|
if (isset($val['order'])) {
|
|
$query .= ' ORDER BY ';
|
|
foreach ($val['order'] as $k => $v) {
|
|
$query .= $k;
|
|
$query .= ' '.strtoupper($v);
|
|
}
|
|
}
|
|
|
|
if ($mode == 'one') {
|
|
$query .= ' LIMIT 1';
|
|
}
|
|
else {
|
|
if (isset($val['limit'])) {
|
|
$query .= ' LIMIT '.(int)$val['limit'];
|
|
}
|
|
}
|
|
|
|
$result = mysqli_query($mysqli, $query.';');
|
|
$row = mysqli_fetch_assoc($result);
|
|
}
|
|
else {
|
|
if ($mode == 'insert') {
|
|
$query .= 'INSERT INTO '.$table.' (';
|
|
foreach ($val['insert'] as $k => $v) {
|
|
$query .= $k;
|
|
if ($k !== array_key_last($val['insert'])) {
|
|
$query .= ', ';
|
|
}
|
|
}
|
|
$query .= ') VALUES (';
|
|
foreach ($val['insert'] as $k => $v) {
|
|
$query .= $v;
|
|
if ($k !== array_key_last($val['insert'])) {
|
|
$query .= ', ';
|
|
}
|
|
}
|
|
$query .= ')';
|
|
}
|
|
else if ($mode == 'update') {
|
|
$query .= 'UPDATE '.$table.' SET ';
|
|
foreach ($val['update'] as $k => $v) {
|
|
$query .= $k.' = '.$v;
|
|
if ($k !== array_key_last($val['update'])) {
|
|
$query .= ', ';
|
|
}
|
|
}
|
|
|
|
if (isset($val['where'])) {
|
|
$query .= ' WHERE ';
|
|
foreach ($val['where'] as $k => $v) {
|
|
$query .= $k.' = '.$v;
|
|
if ($k !== array_key_last($val['where'])) {
|
|
$query .= ' AND ';
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else if ($mode == 'delete') {
|
|
$query .= 'DELETE FROM '.$table;
|
|
|
|
if (isset($val['where'])) {
|
|
$query .= ' WHERE ';
|
|
foreach ($val['where'] as $k => $v) {
|
|
$query .= $k.' = '.$v;
|
|
if ($k !== array_key_last($val['where'])) {
|
|
$query .= ' AND ';
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
$stmt = mysqli_prepare($mysqli, $query.';');
|
|
}
|
|
|
|
return $row;
|
|
}
|
|
?>
|