demo: [链接登录后可见]
效果图:
原始码:
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// get info
$host = $_POST['host'];
$port = $_POST['port'];
$username = $_POST['username'];
$password = $_POST['password'];
$database = $_POST['database'];
$querys = explode(';;', $_POST['querys']);
// connect database
$conn = new mysqli($host, $username, $password, $database, $port);
if ($conn->connect_error) {
die($conn->connect_error);
}
// run sql
$result_list = array();
foreach ($querys as $sql) {
$result = $conn->query($sql);
$dataList = array();
while ($row = $result->fetch_assoc()) {
$dataList[] = $row;
}
$result_list[] = array(
'sql'=> $sql,
'results'=> $dataList
);
}
// close database
$conn->close();
// response data
echo json_encode($result_list);
}
?>
请求方法:
- 发送post请求
- 表单格式
- host指定数据库地址 String
- port指定端口 Int
- username指定用户 String
- password指定密码 String
- database指定数据库 String
- querys指定查询语句 String (多个语句用
;;
隔开)
请求示例:
import requests
data = {
"host": "mysql-r1aproject.d.aivencloud.com",
"port": 27323,
"username": "avnadmin",
"password": "",
"database": "nodeshare",
"querys": "SELECT * from nodes;;SELECT * from nodeshare.nodes"
}
print(requests.post("https://runoneall.serv00.net/Files/mysql-driver.php", data=data).text)
在Cloudflare Worker中使用: (await sqlQuery("SQL")
)
async function sqlQuery(sqls) {
const apiUrl = 'https://runoneall.serv00.net/Files/mysql-driver.php'
const info = {
host: "<地址>",port: <端口>,username: "<用户名>",
password: "<密码>",database: "<数据库名>",querys: sqls};
const formData = new URLSearchParams()
for (const key in info) {if (info.hasOwnProperty(key)) {formData.append(key, info[key])}}
const response = await fetch(apiUrl, {method: "POST",
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
body: formData.toString()})
return response.body
}