<?php #自定义函数 #系统函数 #函数传参 function alert($msg,$url){ die('<script>alert("'.$msg.'");location.href="'.$url.'";</script>'); } #SQL查询函数 function Query ($sql){ #连接数据库 $db = @new mysqli('localhost','sxq','WjiNcC5axxZ4A2HK','shang',3306); #判断是否连接成功 if(mysqli_connect_errno() != 0){ echo ' MySQL 连接失败 .' ; echo mysqli_connect_errno(); } #执行SQL语句 $result = $db->query($sql); #执行关闭语句 $db->close(); #判断SQL中是否包含select ,如果包含则输出结果,否则输出obj数据格式 if (strstr(strtolower($sql),'select') !== FALSE) { return $result->fetch_all(MYSQLI_ASSOC); } else { return $result; } }
时间: 2024-04-05 16:35:08 浏览: 27
根据您提供的代码,您定义了一个名为Query的函数,该函数用于执行SQL查询并返回结果。该函数连接到MySQL数据库,执行查询并返回结果数组。
在该函数中,您使用mysqli扩展连接到数据库,并执行查询。如果查询中包含SELECT语句,函数将使用fetch_all()函数获取所有行的结果,并将其格式化为关联数组。如果查询中不包含SELECT语句,则函数将返回查询结果对象。
该函数还定义了一个名为alert的函数,用于在浏览器中显示警报消息并重定向到指定网址。该函数使用die()函数来终止脚本执行并输出警报消息和重定向脚本。
总体而言,这个函数看起来是用于执行SQL查询并返回结果的通用函数,但是需要注意的是,该函数使用了未经过滤的SQL查询语句,存在SQL注入的风险。建议您使用参数化查询或其他过滤方法来避免这种风险。
相关问题
$.post()的回调函数进行判断并进行页面跳转
好的,针对您的问题,我可以给您提供一些思路和示例代码。
在使用 `$.post()` 方法发送 Ajax 请求时,可以通过回调函数中的 `data` 参数判断后端返回的数据是否符合要求,如果符合要求则进行页面跳转,否则给出错误提示。
以下是一个简单的示例代码,展示了如何在回调函数中进行判断并进行页面跳转:
```javascript
$.post("path/to/your/backend", {param1: "value1", param2: "value2"}, function(data, textStatus, jqXHR) {
if (data.code === 0) {
// 后端返回的数据符合要求,进行页面跳转
window.location.href = "path/to/your/target/page";
} else {
// 后端返回的数据不符合要求,给出错误提示
alert(data.msg);
}
}, "json").fail(function(jqXHR, textStatus, errorThrown) {
// 处理请求失败的情况
alert("请求失败,请稍后再试!");
});
```
在上述代码中,假设后端返回的数据是一个 JSON 对象,其中 `code` 表示请求状态,`0` 表示请求成功,其它值表示请求失败;`msg` 表示错误提示信息。
如果后端返回的 `code` 值为 `0`,则表示请求成功,此时可以通过 `window.location.href` 属性将页面跳转到目标页面;否则,表示请求失败,此时可以通过 `alert()` 方法给出错误提示信息。
希望以上示例代码对您有所帮助,如有疑问请及时提出。
给我一下discuz的自动跳转函数
以下是Discuz的自动跳转函数:
```php
/**
* 自动跳转函数
*
* @param string $url 跳转的URL地址
* @param int $time 等待时间(单位:秒)
* @param string $msg 跳转提示信息
*/
function dheader($url, $time = 0, $msg = '') {
// 防止重复跳转
if (empty($url)) {
return false;
}
if (!empty($msg)) {
$msg = "<script>alert('$msg');</script>";
}
if (!empty($time)) {
$time = "setTimeout(\"window.location.href='$url'\", " . ($time * 1000) . ");";
}
header("Content-type: text/html; charset=utf-8");
header("Cache-control: private");
echo "<html><head><meta http-equiv='Content-Type' content='text/html; charset=utf-8'><meta http-equiv='Cache-Control' content='no-cache'><title>跳转提示</title></head><body>$msg<div style='text-align:center;font-size:14px;margin-top:10px;'>$url</div><script>$time</script></body></html>";
exit();
}
```
使用方法:
```php
// 跳转到百度首页,等待 3 秒,显示“正在跳转到百度...”的提示信息
dheader('http://www.baidu.com', 3, '正在跳转到百度...');
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
<script> layui.use(['upload'], function () { var uploadlnst = layui.upload.render({ elem: '#upload1', type: 'post', method: 'post', url: '/BookShopSystem_war/img', before: function (obj) { //预读本地文件示例,不支持ie8 obj.preview(function (index, file, result) { $("#demo1").attr('src', result); }); }, done: function (res) { if (res.code > 0) { return layer.msg('上传失败'); } alert('上传成功' + res.url); document.getElementById('img_url').value = res.url; }, error: function () { //演示失败状态,重传 var demoText = $('#demoText'); demoText.html('上传失败了 重试'); demoText.find('.demo-reload').on('click', function () { uploadlnst.upload(); }); } }); }); </script>
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)