使用选择准备好的语句获取用户名

时间:2020-02-16 11:15:39

标签: php mysqli prepared-statement

我写了这段代码,但它只是输出“ 1”。它应该输出所选用户的用户名。

$uid = $_GET['id'];
if (empty($uid)) {
    echo "Error: Wrong input of values.";
} else {
    $stmt = $conn->prepare("SELECT username FROM usersinfo WHERE id = ?");
    $stmt->bind_param("s", $uid);
    $stmt->execute();
    $result = $stmt->fetch();
    echo $result;
    $stmt->close();
    $conn->close();
}

2 个答案:

答案 0 :(得分:3)

fetch()不返回结果。该函数没有返回有用的信息。您需要做的就是获取mysqli结果。有两种方法可以做到这一点。

更简单的方法:

$stmt = $conn->prepare("SELECT username FROM usersinfo WHERE id = ?");
$stmt->bind_param("s", $uid);
$stmt->execute();
$result = $stmt->get_result(); // Get the result of the above statement.
$firstRow = $result->fetch_array(); // Get the first result into an array
if ($firstRow) {
    echo $firstRow[0];
    //or
    echo $firstRow['username'];
}

在这里,我们得到mysqli结果对象,然后我们要求一行。

使用bind_result()的另一种方法:

$stmt = $conn->prepare("SELECT username FROM usersinfo WHERE id = ?");
$stmt->bind_param("s", $uid);
$stmt->execute();
$stmt->bind_result($username); // Bind a single column from SELECT to a single variable
$stmt->fetch(); // Fetch the first row. It will populate the variable
echo $username;

我认为这更难以阅读。另外,如果您想获得更多的列,它将很快变得非常复杂。

说实话,mysqli不是很友好,使用起来很麻烦。我强烈建议切换到PDO,但是如果您不能尝试将mysqli函数封装在某些类或函数中。 YCS就是这样的一个例子helper function.,我最近用包装器类编写了一个example of how to extend a mysqli class

我在mysqli类中添加了一个简单的方法,看看语句变得多么简单。

class DBClass extends mysqli {
    public function __construct(
        $host = null,
        $username = null,
        $passwd = null,
        $dbname = null,
        $port = null,
        $socket = null
    ) {
        mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
        parent::__construct($host, $username, $passwd, $dbname, $port, $socket);
        $this->set_charset('utf8mb4');
    }

    public function safeQuery(string $sql, array $params = []): ?array {
        $stmt = $this->prepare($sql);
        if ($params) {
            $stmt->bind_param(str_repeat("s", count($params)), ...$params);
        }
        $stmt->execute();
        if ($result = $stmt->get_result()) {
            return $result->fetch_all(MYSQLI_BOTH);
        }
        return null;
    }
}

用法示例:

$results = $conn->safeQuery("SELECT username FROM users WHERE id = ?", [$uid]);
if($results){
    echo $results[0][0]; // first row, first column 
    // or 
    echo $results[0]['username']; // first row, get column by name
}

答案 1 :(得分:0)

那呢,

我使用了->fetch_assoc();,因此我们将数组中的结果与问题中使用的->fetch();相对

万一用户名重复:-然后

   $uid = $_GET['id'];
    if (empty($uid)) {
        echo "Error: Wrong input of values.";
    } else {
        $stmt = $conn->prepare("SELECT username FROM usersinfo WHERE id = ?");
        $stmt->bind_param("s", $uid);
        $stmt->execute();
        $result = $stmt->get_result();

         while($row = $result->fetch_assoc())  { //fetch_assoc() function fetches a result row as an associative array - here we are putting it in a loop
          echo $row['username'];
         }

        $stmt->close();
        $conn->close();
    }

如果我们只检索一个结果,那么假设没有重复,我们可以在下面使用而无需循环。

   $uid = $_GET['id'];
    if (empty($uid)) {
        echo "Error: Wrong input of values.";
    } else {
        $stmt = $conn->prepare("SELECT username FROM usersinfo WHERE id = ?");
        $stmt->bind_param("s", $uid);
        $stmt->execute();
        $result = $stmt->get_result();

         $row = $result->fetch_assoc(); //fetch_assoc() function fetches a result row as an associative array
         echo $row['username']; //we can specify just to return specific column i.e. username

        $stmt->close();
        $conn->close();
    }
相关问题