【发布时间】:2013-10-09 03:49:27
【问题描述】:
我只需要从 mysql 数据库中获取用户名为 X 的成员的 id。 这只能通过while循环来完成还是有其他方法可以解决这个问题?
我的想法是这样的:
$id = mysqli_query($con,'SELECT id FROM membrs WHERE username = '$username' LIMIT 1)
谢谢,
【问题讨论】:
我只需要从 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)
【讨论】:
$id = mysqli_fetch_array($id);,它可能应该是$show_id = mysqli_fetch_array($id);,然后是echo $show_id['id'];——我说“可能”。看来您只会重置$id
没有while循环我们可以通过下面的代码来实现,如果你选择了超过1条记录,你需要循环它
$row = mysqli_fetch_array($id);
echo $row['id'];
【讨论】: