XF_Index/class/Sql.php

99 lines
3.2 KiB
PHP
Raw Normal View History

2023-05-02 10:30:55 +08:00
<?php
/*
* Copyright © 2016 - 2023 筱锋xiao_lfeng. All Rights Reserved.
* 开发开源遵循 MIT 许可,若需商用请联系开发者
* https://www.x-lf.com/
*/
class Sql
{
/**
* @return false|mysqli
*/
2023-05-02 14:40:45 +08:00
public static function MySqlConn()
{
2023-05-02 10:30:55 +08:00
// 从文件获取数据
$Array_ConfigData = null;
2023-05-02 14:40:45 +08:00
$FileData = fopen(dirname(__FILE__, 2) . "/setting.inc.json", 'r');
2023-05-02 10:30:55 +08:00
while (!feof($FileData))
$Array_ConfigData .= fgetc($FileData);
2023-05-02 14:40:45 +08:00
$Array_ConfigData = json_decode($Array_ConfigData, JSON_UNESCAPED_UNICODE);
2023-05-02 10:30:55 +08:00
fclose($FileData);
//判断数据库端口
2023-05-02 14:40:45 +08:00
if ($Array_ConfigData['Mysql']['Port'] == 3306 or $Array_ConfigData['Mysql']['Port'] == NULL) $Array_ConfigData['Mysql']['Port'] = 3306;
2023-05-02 10:30:55 +08:00
2023-05-02 14:40:45 +08:00
return mysqli_connect($Array_ConfigData['Mysql']['Host'], $Array_ConfigData['Mysql']['Username'], $Array_ConfigData['Mysql']['Password'], null, $Array_ConfigData['Mysql']['Port']);
2023-05-02 10:30:55 +08:00
}
/**
2023-05-04 10:59:20 +08:00
* MySQL查找库 |
* [Tips] 在PHP中Mysql查询语句一次只允许查询一次数据不可多个代码进行连续查询
* @param string $Mysql_Query 输入Mysql查询语句
2023-05-02 10:30:55 +08:00
* @return string[] 查找到结果返回结果
*/
2023-05-02 14:40:45 +08:00
public static function SELECT(string $Mysql_Query): array
{
$CC_i = 0;
$Array_OutPut = [];
if (preg_match('/^SELECT/', $Mysql_Query)) {
$Result = mysqli_query(self::MySqlConn(), $Mysql_Query);
2023-05-04 10:59:20 +08:00
echo mysqli_error(self::MySqlConn());
2023-05-02 14:40:45 +08:00
for (; $Result_Object = mysqli_fetch_object($Result); $CC_i++) {
$Array_OutPut['output'] = 'Success';
$Array_OutPut['data'][$CC_i] = $Result_Object;
2023-05-02 10:30:55 +08:00
}
2023-05-02 14:40:45 +08:00
if ($CC_i == 0)
$Array_OutPut['output'] = 'EmptyResult';
2023-05-04 10:59:20 +08:00
else mysqli_free_result($Result);
2023-05-02 14:40:45 +08:00
} else
2023-05-02 10:30:55 +08:00
$Array_OutPut['output'] = 'TypeError';
2023-05-04 10:59:20 +08:00
mysqli_close(self::MySqlConn());
2023-05-02 14:40:45 +08:00
return $Array_OutPut;
2023-05-02 10:30:55 +08:00
}
/**
* MySQL插入库
2023-05-04 10:59:20 +08:00
* @param string $Mysql_InsertQuery
2023-05-02 10:30:55 +08:00
* @return bool
*/
2023-05-04 10:59:20 +08:00
public static function INSERT(string $Mysql_InsertQuery): bool
2023-05-02 14:40:45 +08:00
{
2023-05-04 10:59:20 +08:00
if (preg_match('/^INSERT/', $Mysql_InsertQuery))
return mysqli_query(self::MySqlConn(), $Mysql_InsertQuery);
else {
mysqli_close(self::MySqlConn());
2023-05-02 10:30:55 +08:00
return false;
2023-05-04 10:59:20 +08:00
}
2023-05-02 10:30:55 +08:00
}
2023-05-02 17:16:50 +08:00
/**
* MySQL更新库
2023-05-04 10:59:20 +08:00
* @param string $Mysql_UpdateQuery
2023-05-02 17:16:50 +08:00
* @return bool
*/
2023-05-04 10:59:20 +08:00
public static function UPDATE(string $Mysql_UpdateQuery): bool
2023-05-02 17:16:50 +08:00
{
2023-05-04 10:59:20 +08:00
if (preg_match('/^UPDATE/', $Mysql_UpdateQuery))
return mysqli_query(self::MySqlConn(), $Mysql_UpdateQuery);
else {
mysqli_close(self::MySqlConn());
2023-05-02 17:16:50 +08:00
return false;
2023-05-04 10:59:20 +08:00
}
2023-05-02 17:16:50 +08:00
}
/**
* MySQL删除库
2023-05-04 10:59:20 +08:00
* @param string $Mysql_DeleteQuery
2023-05-02 17:16:50 +08:00
* @return bool
*/
2023-05-04 10:59:20 +08:00
public static function DELETE(string $Mysql_DeleteQuery): bool
2023-05-02 17:16:50 +08:00
{
2023-05-04 10:59:20 +08:00
if (preg_match('/^DELETE/', $Mysql_DeleteQuery))
return mysqli_query(self::MySqlConn(), $Mysql_DeleteQuery);
else {
mysqli_close(self::MySqlConn());
2023-05-02 17:16:50 +08:00
return false;
2023-05-04 10:59:20 +08:00
}
2023-05-02 17:16:50 +08:00
}
2023-05-02 10:30:55 +08:00
}