I want to do something like this:
$query=mysql_query("SELECT userid FROM users WHERE username='$username'");
$the_user_id= .....
Because all I want is the user ID that corresponds to the username.
The usual way would be that:
$query=mysql_query("SELECT * FROM users WHERE username='$username'");
while ($row = mysql_fetch_assoc($query))
$the_user_id= $row['userid'];
But is there something more efficient that this?
Thanks a lot, regards
You’ve hit the nail right on the head: what you want to do is
SELECT userid FROM users WHERE username='$username'. Try it – it’ll work 🙂SELECT *is almost always a bad idea; it’s much better to specify the names of the columns you want MySQL to return. Of course, you’ll stil need to usemysql_fetch_assocor something similar to actually get the data into a PHP variable, but at least this saves MySQL the overhead of sending all these columns you won’t be using anyway.As an aside, you may want to use the newer mysqli library or PDO instead. These are also much more efficient than the old mysql library, which is being deprecated.