我只需要从 mysql 数据库中获取用户名为 X 的成员的 ID。这只能通过while循环来完成,还是有其他方法可以解决这个问题?
我在想的是这样的:
$id = mysqli_query($con,'SELECT id FROM membrs WHERE username = '$username' LIMIT 1)
谢谢,
您可以使用:
mysqli_fetch_array();
// For Instance
$id_get = mysqli_query($con, "SELECT id FROM membrs WHERE username='$username' LIMIT 1");
$id = mysqli_fetch_array($id_get);
echo $id['id']; // This will echo the ID of that user
// What I use is the following for my site:
$user_get = mysqli_query($con, "SELECT * FROM members WHERE username='$username'");
$user = mysqli_fetch_array($user);
echo $user['username']; // This will echo the Username
echo $user['fname']; // This will echo their first name (I used this for a dashboard)
如果没有 while 循环,我们可以通过以下代码来完成,如果您选择超过 1 条记录,则需要循环它
$row = mysqli_fetch_array($id);
echo $row['id'];