如何检查用户是否在表上存在特定字母

我正在尝试检查用户是否在 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...");

    }


临摹微笑
浏览 133回答 2
2回答

梦里花落0921

首先,欢迎访问 Stackoverflow Beytula Ibryam。那是你的桌子:amx_amxadmins:--------------------------------------------------| ID | USERNAME&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| ACCESS&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;|--------------------------------------------------| 1&nbsp; | TEST1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | abcde&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |--------------------------------------------------| 2&nbsp; | TEST2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | bcde&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;|--------------------------------------------------首先,您需要从您的数据中选择您需要检查的所有用户数据:$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')) {&nbsp; &nbsp;echo("An A has been found!");}else {&nbsp; &nbsp;echo("An A wasn't Found...");}享受 ;)--- 编辑 ---&nbsp; &nbsp; $query = "SELECT * FROM `?` WHERE `username` = ?";&nbsp; &nbsp; $stmt = $mysqli->prepare($query);&nbsp; &nbsp; $stmt->bind_param('ss', $amxadmins, $logged['username']);&nbsp; &nbsp; $stmt->execute();&nbsp; &nbsp; $result = $stmt->get_result();&nbsp; &nbsp; $stmt->close();&nbsp; &nbsp; $obj = $result->fetch_object();&nbsp; &nbsp; if($checkUSRFLAG->num_rows >&nbsp; "a"){&nbsp; &nbsp; &nbsp; &nbsp; echo ("test");&nbsp; &nbsp; }&nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; echo("test2");&nbsp; &nbsp; }如果您没有 MySQLi 连接文件,请将以下几行添加到您的主文件中:$mysqli = new mysqli('YOUR_DATABEAS_IP', 'YOUR_DATABEAS_USER', 'YOUR_DATABEAS_USER_PASSWORD', 'YOUR_DATABEAS_NAME', 'YOUR_DATABEAS_PORT');&nbsp; &nbsp; &nbsp; &nbsp; if($mysqli->connect_error) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exit('Error connecting to database'); //Should be a message a typical user could understand in production&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

江户川乱折腾

听起来您只是在寻找通配符搜索。%为此使用字符。所以而不是这个:AND&nbsp;`access`&nbsp;LIKE&nbsp;'a'你会有这个:AND&nbsp;`access`&nbsp;LIKE&nbsp;'%a%'
打开App,查看更多内容
随时随地看视频慕课网APP