给定 ID,如何获取未知用户名? [英] How can I get an unknown username given an ID?

查看:23
本文介绍了给定 ID,如何获取未知用户名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了一个 20 个字符的数字 ID,看起来像 10527391670258314752,给定这个 ID,我如何获得与之关联的用户名?

I got a numerical ID of 20 characters witch looks like 10527391670258314752, given this ID, how can I get the username associated with it?

表格如下所示:

id                   | name | password | balance   
10527391670258314752 | Jhon | 12345    | 12.51

然后将从数据库中检索到的用户名存储到 $_SESSION['name'] 中.

The username retrieved from the database should then be stored into $_SESSION['name'].

我已经试过了:

$connection = mysqli_connect('localhost', 'root', '', 'user_data');
$id = '10527391670258314752';

$query = "SELECT username FROM user_data WHERE id = '$id'";
$result = mysqli_query($connection, $query);

$record = mysqli_fetch_array($id);
$_SESSION['id'] = $record;

echo $_SESSION['id'];

输出是 Array() 而不是名称.

The output is Array() instead of the name.

推荐答案

我强烈建议您避免使用 mysqli 扩展.你应该使用一些数据库抽象库,而不是直接使用 mysqli 函数;mysqli 类不适合单独使用.

I would strongly recommend that you avoid the mysqli extension. You should use some database abstraction library instead of using the mysqli functions directly; the mysqli class is not suited to be used on its own.

如果你真的想纯粹用 mysqli 类来做,你几乎没有选择.

If you really want to do it purely with the mysqli class, you have few options.

首先,您需要正确打开连接.这 3 行确保您已准备好连接:

First, you need to open the connection properly. These 3 lines ensure you have the connection ready:

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); // enable error reporting
$mysqli = new mysqli('localhost', 'username', 'password', 'dbname');
$mysqli->set_charset('utf8mb4'); // always set the charset

然后你需要准备一个语句,绑定参数并执行.

Then you need to prepare a statement, bind the parameter and execute it.

$id = '10527391670258314752';
$stmt = $mysqli->prepare('SELECT username FROM user_data WHERE id=?');
$stmt->bind_param('s', $id);
$stmt->execute();

执行语句后,您需要获取结果.如果你只有一个变量,你可以简单地使用 bind_result()

Once the statement is executed you need to fetch the results. If you have only one variable you could simply use bind_result()

$stmt->bind_result($_SESSION['name']);
$stmt->fetch();
echo $_SESSION['name'];

但是,不推荐这种方法并且不是很灵活.相反,最好获取整个结果集并获取包含第一行的数组.

However, this approach is not recommended and not very flexible. Instead it's better to fetch the whole result set and get an array containing the first row.

$result = $stmt->get_result();
$row = $result->fetch_array();
$_SESSION['name'] = $row['username'];
echo $_SESSION['name'];


作为 PDO 的替代方案,相同的代码如下所示:


As an alternative with PDO the same code would look like this:

session_start();

$pdo = new PDO("mysql:host=$host;dbname=$db;charset=$charset", $user, $pass, [
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_EMULATE_PREPARES => false,
]);

$id = '10527391670258314752';

$stmt = $pdo->prepare('SELECT username FROM user_data WHERE id=?');
$stmt->execute([$id]);
$_SESSION['name'] = $stmt->fetchColumn();
echo $_SESSION['name'];

这篇关于给定 ID,如何获取未知用户名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆