我正在尝试检查用户在SQL表上是否有特定字母,例如:
我有SQL表amx_amxadmins和列ID,用户名,访问示例:
amx_amxadmins:
--------------------------------------------------
| ID | USERNAME | ACCESS |
--------------------------------------------------
| 1 | TEST1 | abcde |
--------------------------------------------------
| 2 | TEST2 | bcde |
--------------------------------------------------
标志A代表No Access
。
那么,如何检查用户是否具有列访问权限A?
例如:
<?php
$check_user_flag = $mysqli->query("SELECT * FROM `$amxadmins` WHERE `username` = '$logged[username]' AND `access` LIKE 'a' ") or die(mysqli_error($mysqli));
$checkUSRFLAG = $check_user_flag->fetch_assoc();
if($checkUSRFLAG->num_rows > "a"){
echo "test";
}
else {
echo "test2";
}
?>
```
RyffLe编辑
$query = "SELECT * FROM `$amxadmins` WHERE `username` = ?";
$stmt = $mysqli->prepare($query);
$stmt->bind_param('s', $param);
$param = $logged['username'];
$stmt->execute();
$result = $stmt->bind_result();
$stmt->close();
$Permissions = $obj->access;
if (strpos($Permissions, 'a')) {
echo("An A has been found!");
}
else {
echo("An A wasn't Found...");
}
答案 0 :(得分:1)
首先,我对Stackoverflow Beytula Ibryam表示欢迎。
那是您的桌子:
amx_amxadmins:
--------------------------------------------------
| ID | USERNAME | ACCESS |
--------------------------------------------------
| 1 | TEST1 | abcde |
--------------------------------------------------
| 2 | TEST2 | bcde |
--------------------------------------------------
首先,您需要从数据中选择要检查的用户的所有数据:
$query = "SELECT * FROM `$amxadmins` WHERE `username` = ?";
$stmt = $mysqli->prepare($query);
$stmt->bind_param('s', $param);
$param = $logged[username];
$stmt->execute();
$result = $stmt->get_result();
$stmt->close();
$obj = $result->fetch_object();
下一步是获取您作为字符串的访问权限:
$Permissions = $obj->access;
现在,我们可以检查您想要的文字的访问权限:
if (strpos($Permissions, 'a')) {
echo("An A has been found!");
}
else {
echo("An A wasn't Found...");
}
享受;)
---编辑---
$query = "SELECT * FROM `?` WHERE `username` = ?";
$stmt = $mysqli->prepare($query);
$stmt->bind_param('ss', $amxadmins, $logged['username']);
$stmt->execute();
$result = $stmt->get_result();
$stmt->close();
$obj = $result->fetch_object();
if($checkUSRFLAG->num_rows > "a"){
echo ("test");
}
else {
echo("test2");
}
如果没有MySQLi连接文件,请在主文件中添加以下行:
$mysqli = new mysqli('YOUR_DATABEAS_IP', 'YOUR_DATABEAS_USER', 'YOUR_DATABEAS_USER_PASSWORD', 'YOUR_DATABEAS_NAME', 'YOUR_DATABEAS_PORT');
if($mysqli->connect_error) {
exit('Error connecting to database'); //Should be a message a typical user could understand in production
}
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
答案 1 :(得分:0)
听起来您只是在寻找通配符搜索。为此使用%
字符。所以代替这个:
AND `access` LIKE 'a'
您将拥有:
AND `access` LIKE '%a%'