MYSQLI query to get one single result [duplicate]

我与影子孤独终老i 提交于 2019-11-30 20:07:27

问题


I need to get only the id of member who's username is X from the mysql database. Can this only be done with a while loop or is there any other way around this?

What I'm thinking of is something like:

$id = mysqli_query($con,'SELECT id FROM membrs WHERE username = '$username' LIMIT 1)

Thanks,


回答1:


You can use:

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)



回答2:


without while loop we can do it by the following code, if you are selecting more than 1 record you need to loop it

$row = mysqli_fetch_array($id);
echo $row['id'];


来源:https://stackoverflow.com/questions/19262748/mysqli-query-to-get-one-single-result

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!