4

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,

rokkz
  • 179
  • 1
  • 1
  • 13

2 Answers2

9

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)
Brian Logan
  • 812
  • 2
  • 6
  • 20
  • Why down vote? Just out of curiosity – Brian Logan Oct 09 '13 at 03:56
  • I did not downvote, but probably because you're using `$id = mysqli_fetch_array($id);` where it should probably be `$show_id = mysqli_fetch_array($id);` then `echo $show_id['id'];` - I said "probably". It seems like you'd only be resetting `$id` – Funk Forty Niner Oct 09 '13 at 03:58
  • I normally don't do that. I will update the answer. – Brian Logan Oct 09 '13 at 04:00
  • And now, we wait for the OP to test/decide. – Funk Forty Niner Oct 09 '13 at 04:03
  • Yes, did you post the other answer? It shows user.... not Fred -ii- – Brian Logan Oct 09 '13 at 04:07
  • No, wasn't me. Shows as [`user2092317`](http://stackoverflow.com/users/2092317/user2092317) – Funk Forty Niner Oct 09 '13 at 04:08
  • Thought maybe it was you, just not logged in yet. haha – Brian Logan Oct 09 '13 at 04:08
  • 1
    I rarely put in answers these days, as I am learning MySQL/PDO (3 months now) and doing rather well for the amount of time so far. Only when I am 100% sure/confident on the subject, will I submit something. Otherwise, I put in comments when I'm 75% sure ;-) – Funk Forty Niner Oct 09 '13 at 04:12
  • HAHA, I am going to have to start learning PDO, I know MySQLi, and kinda MySQL, but not PDO. I am also doing a lot of stuff using AJAX and JS pulling in JSON and parsing it to make it so the page will load, then content, and while content is loading, have a loading symbol. Adds a nicer effect and makes the person think the site is not broken. – Brian Logan Oct 09 '13 at 15:29
  • It's a good idea to have knowledge of PDO also. It's apparently more secure, yet it is slightly more complicated, but that's where the fun in a challenge is ;-) – Funk Forty Niner Oct 09 '13 at 15:32
  • Yea, that is why I have been using AJAX, and posting to the URL and getting a JSON response. I use API keys and whatnot to authenticate that it is me connecting, and not some 3rd party. – Brian Logan Oct 09 '13 at 15:37
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'];
user2092317
  • 3,226
  • 5
  • 24
  • 35