From c1948a44dcb4ddc3c24549d1d7c3a914f1c513f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A9=99=E6=A9=99=E6=A5=8A?= <2370374436@qq.com> Date: Sat, 26 Oct 2019 00:28:04 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=8F=90=E7=A4=BA=20?= =?UTF-8?q?=E5=9E=83=E5=9C=BE=E6=90=9C=E7=B4=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Log.php | 4 +- admin/ApiConfig.php | 22 ++++--- admin/Login.php | 9 ++- admin/Manager.php | 9 ++- admin/Question.php | 30 ++++++--- admin/WasteSorting.php | 130 +++++++++++++++++++++++++++------------ api/BaiduImage.php | 33 ++++++---- api/QueryText.php | 46 +++++--------- api/QuestionAPI.php | 19 ++++-- api/test.php | 5 ++ baidu/BaiduAiRequest.php | 21 +++---- database/DbUtil.php | 77 +++++++++++++---------- database/Query.php | 126 ++++++++++--------------------------- test.php | 6 ++ 14 files changed, 289 insertions(+), 248 deletions(-) create mode 100644 api/test.php diff --git a/Log.php b/Log.php index ea658f8..a17dae8 100644 --- a/Log.php +++ b/Log.php @@ -3,6 +3,7 @@ require_once $_SERVER["DOCUMENT_ROOT"] . '/vendor/autoload.php'; use Monolog\Formatter\LineFormatter; +use Monolog\Handler\RotatingFileHandler; use Monolog\Handler\StreamHandler; use Monolog\Logger; @@ -30,8 +31,9 @@ function getStream($path) function getLogger() { $log = new Logger('name'); - $log->pushHandler(getStream($_SERVER["DOCUMENT_ROOT"] . "/log/run.log")); +// $log->pushHandler(getStream($_SERVER["DOCUMENT_ROOT"] . "/log/run.log")); $log->pushHandler(getStream("php://stdout")); // <<< uses a stream + $log->pushHandler(new RotatingFileHandler($_SERVER["DOCUMENT_ROOT"] . "/log/run.log", 7)); return $log; } diff --git a/admin/ApiConfig.php b/admin/ApiConfig.php index 6e4e18f..bec4aee 100644 --- a/admin/ApiConfig.php +++ b/admin/ApiConfig.php @@ -10,11 +10,15 @@ require_once $_SERVER["DOCUMENT_ROOT"] . "/baidu/BaiduAiRequest.php"; if (empty($_REQUEST) or !empty($_GET["check"])) { - - $result = BaiduAiRequest::get_token_config(); - if (!empty($result)) { - $api_key = $result["api_key"]; - $secret_key = $result["secret_key"]; + if (!empty($_GET["check"]) && !empty($_GET["api_key"]) && !empty($_GET["secret_key"])) { + $api_key = $_GET["api_key"]; + $secret_key = $_GET["secret_key"]; + } else { + $result = BaiduAiRequest::get_token_config(); + if (!empty($result)) { + $api_key = $result["api_key"]; + $secret_key = $result["secret_key"]; + } } echo <<
- API Key + Secret Key
@@ -88,14 +92,14 @@ EOF; $secret_key = $_POST["secret_key"]; $result = BaiduAiRequest::request_token_with_config($api_key, $secret_key); if (array_key_exists("status", $result) and $result["status"]) { - info_res("AK/SK测试通过", $_SERVER["PHP_SELF"] . "?check=true"); + info_res("AK/SK测试通过", $_SERVER["PHP_SELF"] . "?check=true&api_key=" . $_POST["api_key"] . "&secret_key=" . $_POST["secret_key"]); } else { warn_res("AK/SK测试失败", $_SERVER["PHP_SELF"] . "?check=false"); } } else if ($_GET["action"] == "update") { try { - DbUtil::update("update param set param_value=? where param_key=?", "ss", $_POST["api_key"], "api_key"); - DbUtil::update("update param set param_value=? where param_key=?", "ss", $_POST["secret_key"], "secret_key"); + DbUtil::update("update param set param_value=? where param_key=?", null, "ss", $_POST["api_key"], "api_key"); + DbUtil::update("update param set param_value=? where param_key=?", null, "ss", $_POST["secret_key"], "secret_key"); info_res("保存成功", $_SERVER["PHP_SELF"]); } catch (Exception $e) { error($e); diff --git a/admin/Login.php b/admin/Login.php index 8e456e8..90bb3f7 100644 --- a/admin/Login.php +++ b/admin/Login.php @@ -17,7 +17,7 @@ function update_token(QueryManager $manager_result, $msg) $manager = $manager_result->getManagerResult(); $manager->setLastIp(get_remote_ip()); $manager->setLastTime(date(default_format)); - DbUtil::update("update manager set last_ip=?,last_time=? where manager_name=?", "sss", $manager->getLastIp(), $manager->getLastTime(), $manager->getManagerName()); + DbUtil::update("update manager set last_ip=?,last_time=? where manager_name=?", null, "sss", $manager->getLastIp(), $manager->getLastTime(), $manager->getManagerName()); // 跳转到首页 if ($_SERVER["SCRIPT_NAME"] == "/admin/Manager.php") { @@ -57,7 +57,12 @@ function check_login() { if (!empty($_COOKIE["login_token"]) and !empty($_COOKIE["manager_name"])) { $manager_result = new QueryManager(); - DbUtil::query("select * from manager where manager_name=?", $manager_result, array($_COOKIE["manager_name"])); + try { + DbUtil::query("select * from manager where manager_name=?", $manager_result, array($_COOKIE["manager_name"])); + } catch (Exception $e) { + error_res("系统出现严重异常,请联系管理员", "/"); + return false; + } // 验证cookie身份信息有效性 if (empty($manager_result) or $_COOKIE["login_token"] != $manager_result->getManagerResult()->loginToken()) { setcookie("manager_name", "", time() - 3600, "/"); diff --git a/admin/Manager.php b/admin/Manager.php index 27fa60f..ad1a77e 100644 --- a/admin/Manager.php +++ b/admin/Manager.php @@ -32,7 +32,7 @@ $(function() {
-
+
@@ -87,7 +87,12 @@ EOF; error_res(array("status" => false, "msg" => "管理员密码不能为空"), "/admin/Manager.php"); } else { $manager_result = new QueryManager(); - DbUtil::query("select * from manager where manager_name=? and manager_pwd=?", $manager_result, array($_POST["manager_name"], md5($_POST["manager_pwd"]))); + try { + DbUtil::query("select * from manager where manager_name=? and manager_pwd=?", $manager_result, array($_POST["manager_name"], md5($_POST["manager_pwd"]))); + } catch (Exception $e) { + error_res("系统出现严重异常,请联系管理员", "/"); + return; + } if (empty($manager_result->getManagerResult())) { error_res("登陆失败,管理员或密码错误", "/admin/Manager.php"); } else { diff --git a/admin/Question.php b/admin/Question.php index 4f6eb96..a32655a 100644 --- a/admin/Question.php +++ b/admin/Question.php @@ -5,13 +5,13 @@ namespace admin; use Answer; -use AnswerQuery; use DbUtil; use DoConn; use Exception; use mysqli; +use QueryAnswer; +use QueryQuestion; use Question; -use QuestionQuery; require_once $_SERVER["DOCUMENT_ROOT"] . "/config.php"; require_once $_SERVER["DOCUMENT_ROOT"] . "/head.php"; @@ -73,8 +73,13 @@ if (empty($_REQUEST)) {
tip:选中题目查看选项
EOF; - $query = new QuestionQuery(); - DbUtil::query("select * from question", $query); + $query = new QueryQuestion(); + try { + DbUtil::query("select * from question", $query); + } catch (Exception $e) { + error_res("系统出现严重异常,请联系管理员", "/"); + return; + } if (empty($query->getQuestionArray())) { warn_res_with_click("题库暂无题目,需要添加题目", <<getAnswerArray())) { error_res("问题id不合法", "/admin/Question.php"); } else { @@ -266,7 +276,7 @@ EOF; error_res("非法参数id", "/admin/Question.php"); } else { try { - DbUtil::delete("DELETE q,a FROM question as q,answer as a WHERE q.question_id=a.question_id and q.question_id=?", "i", (int)$_GET["question_id"]); + DbUtil::delete("DELETE q,a FROM question as q,answer as a WHERE q.question_id=a.question_id and q.question_id=?", null, "i", (int)$_GET["question_id"]); info_res("问题删除成功", "/admin/Question.php"); } catch (Exception $e) { error($e); diff --git a/admin/WasteSorting.php b/admin/WasteSorting.php index 879bc04..e6c3237 100644 --- a/admin/WasteSorting.php +++ b/admin/WasteSorting.php @@ -16,7 +16,6 @@ if (empty($_REQUEST)) {
+
+EOF; + echo ''; + echo << + +
+
+ 垃圾名 +
+ +
+ 垃圾名不为空 +
+ +
+ +
@@ -165,12 +221,13 @@ EOF; + EOF; - foreach ($garbage_query->getResultList() as $garbage) { + foreach ($garbage_query->getGarbageObjArray() as $garbage) { if ($garbage instanceof Garbage) { $name = $garbage->getName(); $category = get_category($garbage->getCategory()); @@ -182,8 +239,9 @@ EOF; - EOF; + echo ''; + } } echo << - 上一页 - -EOF; + echo '
  • 上一页
  • '; } for ($start_page = $page; $start_page <= $max_page and $start_page < $page + 3; $start_page++) { - echo '
  • ' . $start_page . '
  • '; + echo '
  • ' . $start_page . '
  • '; } if ($page != $max_page) { $next = $page + 1; - echo << - 下一页 - -EOF; + echo '
  • 下一页
  • '; } else { echo << @@ -226,15 +276,19 @@ EOF; EOF; } - echo << - -
    - 返回 -
    - - - EOF; + echo ''; + } + } +} else if (isset($_GET["action"]) && $_GET["action"] == "delete") { + if (empty($_GET["name"])) { + error_res("缺少垃圾名,无法删除!", $_SERVER["PHP_SELF"]); + } else { + try { + DbUtil::delete("delete from garbage where name=?", null, "s", $_GET["name"]); + info_res("删除成功", $_SERVER["PHP_SELF"]); + } catch (Exception $e) { + error($e); + error_res("删除失败,请联系管理员", $_SERVER["PHP_SELF"]); } } } else { diff --git a/api/BaiduImage.php b/api/BaiduImage.php index 200df27..1bcfb18 100644 --- a/api/BaiduImage.php +++ b/api/BaiduImage.php @@ -28,11 +28,16 @@ function query_waste_soring($image_res) $sql = "select * from garbage where" . substr($sql, 3); info("查询sql" . $sql); - $garbage_result = QueryGarbageWithName($sql, $keyword); - json_res(json_encode(array("img_parse" => true, "query" => true, "img_res" => $image_res, "result" => $garbage_result->getGarbageObjArray()), JSON_UNESCAPED_UNICODE)); + $garbage_result = new QueryGarbage(); + try { + DbUtil::query($sql, $garbage_result, $keyword); + json_res(json_encode(array("img_parse" => true, "query" => true, "img_res" => $image_res, "result" => $garbage_result->getGarbageObjArray()), JSON_UNESCAPED_UNICODE)); + } catch (Exception $e) { + json_res(json_encode(array("img_parse" => true, "query" => false, "img_res" => $image_res, "error" => "查询异常,请联系管理员"), JSON_UNESCAPED_UNICODE)); + } } else { - json_res(json_encode(array("img_parse" => true, "img_res" => $image_res))); + json_res(json_encode(array("img_parse" => true, "img_res" => $image_res, "query" => false, "error" => "没有查询结果"))); } } else { json_res(json_encode(array("img_parse" => false))); @@ -41,21 +46,25 @@ function query_waste_soring($image_res) // 查询token -$token_result = new class() extends AbstractTokenQuery -{ - public function bind_param(mysqli_stmt $stmt, array $param = null) - { - - } -}; -DbUtil::query("select * from api_token limit 0,1", $token_result); +$token_result = new QueryToken(); +try { + DbUtil::query("select * from api_token limit 0,1", $token_result); +} catch (Exception $e) { + json_res(json_encode(array("img_parse" => false, "msg" => "无法调用百度API,请联系管理员"))); + return; +} // toekn如果失效,则更新token if (strtotime(date(default_format)) >= strtotime($token_result->getToken()->getEndTime())) { info("token失效了,更新token"); $result = BaiduAiRequest::request_token(); if (array_key_exists("status", $result) and $result["status"]) { - DbUtil::query("select * from api_token limit 0,1", $token_result); + try { + DbUtil::query("select * from api_token limit 0,1", $token_result); + } catch (Exception $e) { + json_res(json_encode(array("img_parse" => false, "msg" => "无法调用百度API,请联系管理员"))); + return; + } } else { json_res(json_encode(array("img_parse" => false, "msg" => "无法调用百度API,请联系管理员"))); return; diff --git a/api/QueryText.php b/api/QueryText.php index 53ca3c7..361725d 100644 --- a/api/QueryText.php +++ b/api/QueryText.php @@ -4,43 +4,27 @@ require_once $_SERVER["DOCUMENT_ROOT"] . "/config.php"; require_once $_SERVER["DOCUMENT_ROOT"] . "/api/JsonResponse.php"; require_once $_SERVER["DOCUMENT_ROOT"] . "/database/DbUtil.php"; require_once $_SERVER["DOCUMENT_ROOT"] . "/database/Query.php"; +require_once $_SERVER["DOCUMENT_ROOT"] . "/Log.php"; -//关键字查询 -class QueryText -{ - public static function get($sql, $keyword): AbstractGarbageQuery - { - $garbage_result = new class() extends AbstractGarbageQuery - { - -// 参数绑定 - public function bind_param(mysqli_stmt $stmt, array $param = null) - { - if (count($param) > 1) { - $stmt->bind_param(str_repeat("s", count($param)), $param[0], ...array_slice($param, 1)); - } else if (count($param) == 1) { - $stmt->bind_param(str_repeat("s", count($param)), $param[0]); - } - - } - }; - - - DbUtil::query($sql, $garbage_result, $keyword); - - return $garbage_result; - } -} - if (empty($_GET)) { json_res(json_encode(array("status" => false, "error" => "非法请求"), JSON_UNESCAPED_UNICODE)); } else if (isset($_GET["keyword"]) and !empty($_GET["keyword"])) { - $result = QueryGarbageWithName("select * from garbage where name like ?", array("%" . $_GET["keyword"] . "%")); - json_res(json_encode(array("status" => true, "result" => $result->getGarbageObjArray(), "keyword" => $_GET["keyword"]), JSON_UNESCAPED_UNICODE)); + $query_result = new QueryGarbage(); + try { + DbUtil::query("select * from garbage where name like ?", $query_result, array("%" . $_GET["keyword"] . "%")); + json_res(json_encode(array("status" => true, "result" => $query_result->getGarbageObjArray(), "keyword" => $_GET["keyword"]), JSON_UNESCAPED_UNICODE)); + } catch (Exception $e) { + json_res(json_encode(array("status" => false, "error" => "系统出现严重异常,请联系管理员"), JSON_UNESCAPED_UNICODE)); + } } else if (isset($_GET["category"]) and is_numeric($_GET["category"])) { - $result = QueryGarbageWithName("select * from garbage where category = ?", array((int)$_GET["category"])); - json_res(json_encode(array("status" => true, "result" => $result->getGarbageObjArray(), "category" => $_GET["category"]), JSON_UNESCAPED_UNICODE)); + $query_result = new QueryGarbage(); + try { + DbUtil::query("select * from garbage where category = ?", $query_result, array((int)$_GET["category"])); + json_res(json_encode(array("status" => true, "result" => $query_result->getGarbageObjArray(), "category" => $_GET["category"]), JSON_UNESCAPED_UNICODE)); + } catch (Exception $e) { + json_res(json_encode(array("status" => false, "error" => "系统出现严重异常,请联系管理员"), JSON_UNESCAPED_UNICODE)); + } } else { json_res(json_encode(array("status" => false, "error" => "非法查询参数"), JSON_UNESCAPED_UNICODE)); } \ No newline at end of file diff --git a/api/QuestionAPI.php b/api/QuestionAPI.php index a26f758..8e67555 100644 --- a/api/QuestionAPI.php +++ b/api/QuestionAPI.php @@ -7,18 +7,27 @@ require_once $_SERVER["DOCUMENT_ROOT"] . "/database/Query.php"; //题库题目数上限 $max_count = 10; -$q_result = new QuestionQuery(); +$q_result = new QueryQuestion(); //随机抽取问题生成题库 -DbUtil::query("select * from question order by rand() limit 0," . $max_count, $q_result); +try { + DbUtil::query("select * from question order by rand() limit 0," . $max_count, $q_result); +} catch (Exception $e) { + json_res(json_encode(array("status" => false, "msg" => "生成题库出现严重异常"), JSON_UNESCAPED_UNICODE)); + return; +} if (empty($q_result->getQuestionArray())) { - json_res(json_encode(array(["status" => false, "msg" => "生成题库失败,题库为空,请联系管理员添加题目"]), JSON_UNESCAPED_UNICODE)); + json_res(json_encode(array("status" => false, "msg" => "生成题库失败,题库为空,请联系管理员添加题目"), JSON_UNESCAPED_UNICODE)); return; } else { foreach ($q_result->getQuestionArray() as $querstion_item) { if ($querstion_item instanceof Question) { - $answer_res = new AnswerQuery(); - DbUtil::query("select * from answer where question_id=? order by rand()", $answer_res, array($querstion_item->getQuestionId())); + $answer_res = new QueryAnswer(); + try { + DbUtil::query("select * from answer where question_id=? order by rand()", $answer_res, array($querstion_item->getQuestionId())); + } catch (Exception $e) { + json_res(json_encode(array("status" => false, "msg" => "生成题库出现严重异常,请联系管理员"), JSON_UNESCAPED_UNICODE)); + } if (empty($answer_res->getAnswerArray())) { error("无法查询question_id=" . $querstion_item->getQuestionId() . "答案"); json_res(json_encode(array(["status" => false, "msg" => "生成题库失败,请联系管理员"]), JSON_UNESCAPED_UNICODE)); diff --git a/api/test.php b/api/test.php new file mode 100644 index 0000000..a814bab --- /dev/null +++ b/api/test.php @@ -0,0 +1,5 @@ +getParamList())) { $config_result = $config_query->getParamList(); if (array_key_exists("api_key", $config_result) && array_key_exists("secret_key", $config_result)) { @@ -27,7 +32,7 @@ class BaiduAiRequest public static function request_token() { $config_result = self::get_token_config(); - if (empty($config_result)) { + if (is_null($config_result) || empty($config_result)) { error("初始化配置失败,请联系管理员"); // return << "初始化配置失败,请联系管理员", "status" => false); - } else { $post_data['grant_type'] = 'client_credentials'; $post_data['client_id'] = $apikey; $post_data['client_secret'] = $secret_key; @@ -61,12 +57,11 @@ class BaiduAiRequest DbUtil::delete("delete from api_token"); $token = new ApiToken(null, $json_res["refresh_token"], $json_res["expires_in"], null, $json_res["scope"], $json_res["session_key"], $json_res["access_token"], $json_res["session_secret"]); DbUtil::insert("insert into api_token (refresh_token, expires_in, end_time, scope, session_key, access_token, session_secret) values (?,?,?,?,?,?,?)", - $types = "sisssss", $token->getRefreshToken(), $token->getExpiresIn(), date_format(date_create(date(default_format))->add(date_interval_create_from_date_string($token->getExpiresIn() . " seconds")), default_format), $token->getScope(), $token->getSessionKey(), $token->getAccessToken(), $token->getSessionSecret()); + null, "sisssss", $token->getRefreshToken(), $token->getExpiresIn(), date_format(date_create(date(default_format))->add(date_interval_create_from_date_string($token->getExpiresIn() . " seconds")), default_format), $token->getScope(), $token->getSessionKey(), $token->getAccessToken(), $token->getSessionSecret()); info("token保存成功"); // return $token; return array("msg" => "百度API请求成功", "status" => true, "token" => $token); } - } } catch (Exception $e) { error($e); // return <<connect_error) { - error("数据库连接失败"); + error("数据库连接失败,请检查数据库配置" . "服务器:" . db_config["servername"] . ",用户名:" . db_config["username"] . ",密码:" . db_config["password"] . ",数据库:" . db_config["dbname"] . "是否正确"); + throw new Exception("数据库连接失败"); } else { info("数据库连接成功"); } return $conn; } catch (Exception $e) { - error("数据库连接异常"); - die("数据库连接异常"); + error("数据库连接异常" . $e); } } @@ -52,49 +52,55 @@ class DbUtil } // 插入数据 - public static function modify($action, $sql, DoConn $do_conn = null, $types = "", ...$_) + public static function modify($action, $sql, DoConn $do_conn = null, $types = null, ...$_) { try {// 创建连接 $conn = self::getConn(); - $stmt = $conn->prepare($sql);//创建一个预定义的对象 ?占位 - if (!empty($types)) { - $stmt->bind_param($types, ...$_);// 参数绑定 + if (!is_null($conn)) { + $stmt = $conn->prepare($sql);//创建一个预定义的对象 ?占位 + if ($stmt instanceof mysqli_stmt) { + if (!is_null($types)) { + + if (strlen($types) == count($_)) { + $stmt->bind_param($types, ...$_);// 参数绑定 + } else { + throw new Exception("预处理sql" . $sql . ",参数绑定类型" . $types . "跟参数数量" . var_dump($_) . "不匹配"); + } + } + $stmt->execute();// 执行sql + info($action . $stmt->affected_rows . "条数据"); + $stmt->close(); + info("执行sql.$sql.成功"); + if (!is_null($do_conn)) { + $do_conn->before_close($conn); + } + } else { + throw new Exception("预处理sql异常,无法处理sql" . $sql . "."); + } + self::closeConn($conn);// 关闭连接 } - $stmt->execute();// 执行sql - info($action . $stmt->affected_rows . "条数据"); - $stmt->close(); - info("执行sql.$sql.成功"); - if (!empty($do_conn)) { - $do_conn->before_close($conn); - } - self::closeConn($conn);// 关闭连接 } catch (Exception $e) { error($action . "数据异常" . $e); } } // 插入数据 - public static function insert($sql, $types = "", ...$_) - { - self::modify("插入", $sql, null, $types, ...$_); - } - - public static function insert_with_param($sql, $types, $db_conn, ...$_) + public static function insert($sql, $db_conn = null, $types = null, ...$_) { self::modify("插入", $sql, $db_conn, $types, ...$_); } // 更新数据 - public static function update($sql, $types = "", ...$_) + public static function update($sql, $db_conn = null, $types = null, ...$_) { - self::modify("更新", $sql, null, $types, ...$_); + self::modify("更新", $sql, $db_conn, $types, ...$_); } // 删除数据 - public static function delete($sql, $types = "", ...$_) + public static function delete($sql, $db_conn = null, $types = null, ...$_) { - self::modify("删除", $sql, null, $types, ...$_); + self::modify("删除", $sql, $db_conn, $types, ...$_); } @@ -103,16 +109,25 @@ class DbUtil { try {// 创建连接 $conn = self::getConn(); - $stmt = $conn->prepare($sql);//创建一个预定义的对象 ?占位 - $doExcute->bind_param($stmt, $param); - $stmt->execute();// 执行sql - $doExcute->doResult($stmt); - info("执行sql.$sql.成功"); - $stmt->close(); + if (!is_null($conn)) { + $stmt = $conn->prepare($sql);//创建一个预定义的对象 ?占位 + if ($stmt instanceof mysqli_stmt) { + $doExcute->bind_param($stmt, $param); + $stmt->execute();// 执行sql + $doExcute->doResult($stmt); + info("执行sql.$sql.成功"); + $stmt->close(); + } else { + throw new Exception("预处理sql异常,无法处理sql" . $sql . "."); + } + } else { + throw new Exception("无法连接数据库"); + } // 关闭连接 self::closeConn($conn); } catch (Exception $e) { error("查询数据异常" . $e); + throw $e; } } } \ No newline at end of file diff --git a/database/Query.php b/database/Query.php index f6c9e59..b8f5936 100644 --- a/database/Query.php +++ b/database/Query.php @@ -7,8 +7,32 @@ require_once $_SERVER["DOCUMENT_ROOT"] . "/entity/Answer.php"; require_once $_SERVER["DOCUMENT_ROOT"] . "/entity/Manager.php"; require_once $_SERVER["DOCUMENT_ROOT"] . "/entity/Param.php"; require_once $_SERVER["DOCUMENT_ROOT"] . "/database/DbUtil.php"; + +abstract class BindParam implements DoExcute +{ + public function bind_param(mysqli_stmt $stmt, array $param = null) + { + if (!is_null($param)) { + $types = ""; + foreach ($param as $p) { + if (is_double($p)) { + $types .= "d"; + } else if (is_numeric($p)) { + $types .= "i"; + } else if (is_string($p)) { + $types .= "s"; + } else { + $types .= "b"; + } + } + $stmt->bind_param($types, $param[0], ...array_slice($param, 1)); + } + } +} + + //Token查询 -abstract class AbstractTokenQuery implements DoExcute +class QueryToken extends BindParam { // token查询结果 private $token_array = array(); @@ -58,7 +82,7 @@ abstract class AbstractTokenQuery implements DoExcute } //垃圾分类查询 -abstract class AbstractGarbageQuery implements DoExcute +class QueryGarbage extends BindParam { // 垃圾分类查询结果 private $garbage_obj_array = array(); @@ -85,38 +109,13 @@ abstract class AbstractGarbageQuery implements DoExcute } -//根据垃圾名模糊匹配垃圾分类信息 -function QueryGarbageWithName($sql, $keyword): AbstractGarbageQuery -{ - $garbage_result = new class() extends AbstractGarbageQuery - { - public function bind_param(mysqli_stmt $stmt, array $param = null) - { - if (count($param) > 1) { - $stmt->bind_param(str_repeat("s", count($param)), $param[0], ...array_slice($param, 1)); - } else { - $stmt->bind_param(str_repeat("s", count($param)), $param[0]); - } - - } - }; - - DbUtil::query($sql, $garbage_result, $keyword); - - return $garbage_result; -} //查询问题 -class QuestionQuery implements DoExcute +class QueryQuestion extends BindParam { private $question_array = array(); - public function bind_param(mysqli_stmt $stmt, array $param = null) - { - - } - public function doResult(mysqli_stmt $stmt) { $stmt->bind_result($col1, $col2); @@ -137,17 +136,11 @@ class QuestionQuery implements DoExcute } //查询问题答案 -class AnswerQuery implements DoExcute +class QueryAnswer extends BindParam { private $answer_array = array(); - public function bind_param(mysqli_stmt $stmt, array $param = null) - { - if (!empty($param)) { - $stmt->bind_param("i", $param[0]); - } - } public function doResult(mysqli_stmt $stmt) { @@ -185,15 +178,10 @@ function get_category($category) } //统计垃圾分类数据量 -class CountGrabage implements DoExcute +class CountGrabage extends BindParam { private $result = array(); - public function bind_param(mysqli_stmt $stmt, array $param = null) - { - - } - public function doResult(mysqli_stmt $stmt) { $stmt->bind_result($col1, $col2); @@ -213,48 +201,12 @@ class CountGrabage implements DoExcute } -//查询某个垃圾分类下的垃圾数据 -class QueryGarbageWithCategory implements DoExcute -{ - private $result_list = array(); - - public function bind_param(mysqli_stmt $stmt, array $param = null) - { - if (!empty($param)) { - $stmt->bind_param("iii", $param[0], ...array_slice($param, 1)); - } - } - - public function doResult(mysqli_stmt $stmt) - { - $stmt->bind_result($col1, $col2, $col3, $col4); - while ($stmt->fetch()) { - array_push($this->result_list, new Garbage($col1, $col2, $col3, $col4)); - } - } - - /** - * @return array - */ - public function getResultList(): array - { - return $this->result_list; - } -} - //查询某个垃圾分类数据量 -class QueryGarbageCount implements DoExcute +class QueryGarbageCount extends BindParam { private $count; - public function bind_param(mysqli_stmt $stmt, array $param = null) - { - if (!empty($param)) { - $stmt->bind_param("i", $param[0]); - } - } - public function doResult(mysqli_stmt $stmt) { $stmt->bind_result($col1); @@ -275,18 +227,11 @@ class QueryGarbageCount implements DoExcute } //查询管理 -class QueryManager implements DoExcute +class QueryManager extends BindParam { private $manager_result; - public function bind_param(mysqli_stmt $stmt, array $param = null) - { - if (!empty($param)) { - $stmt->bind_param(str_repeat("s", count($param)), $param[0], ...array_slice($param, 1)); - } - } - public function doResult(mysqli_stmt $stmt) { $stmt->bind_result($col1, $col2, $col3, $col4); @@ -307,17 +252,10 @@ class QueryManager implements DoExcute } //查询系统参数 -class QueryParam implements DoExcute +class QueryParam extends BindParam { private $param_list = array(); - public function bind_param(mysqli_stmt $stmt, array $param = null) - { - if (!empty($param)) { - $stmt->bind_param("sss", $param[0], ...array_slice($param, 1)); - } - } - public function doResult(mysqli_stmt $stmt) { $stmt->bind_result($col1, $col2, $col3); diff --git a/test.php b/test.php index 70bfe42..fcdb5f2 100644 --- a/test.php +++ b/test.php @@ -28,4 +28,10 @@ //$s2 = "dotcoo"; //$s3 = "hello dotcoo"; //var_dump(substr($s1, -strlen($s2)) === $s2); +function insert($sql, $db_conn = null, $types = null, ...$_) +{ + var_dump($types); +} + +insert("123", $types = "ffff");
    垃圾分类 创建时间 操作
    $category $create_at