Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MYSQLI query to get one single result [duplicate]

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,

like image 391
rokkz Avatar asked Oct 28 '25 20:10

rokkz


1 Answers

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)
like image 99
Brian Logan Avatar answered Oct 30 '25 12:10

Brian Logan