Link to home
Start Free TrialLog in
Avatar of benwiggy
benwiggyFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Easy question: Collect data from mySQL database

If have a table, lets say phpbb_users, and it has the fields username & user_id.
If I have the username stored as a variable, how can I find the corresponding user_id and store it as a variable?

{ Moved to PHP & Databases - Diablo84/PHP Page Editor }
Avatar of punkstar
punkstar
Flag of United Kingdom of Great Britain and Northern Ireland image

create a php function

<?php
$connect = mysql_connect($mysql_server,$mysql_username,$mysql_password);
mysql_select_db($mysql_dbname);

function username2userid($username)
{
  global $connect;
  if(!isset($username))
      {
        return false;
      }
      else
      {
        $sql = "SELECT * FROM `phpbb_users` WHERE `username` = '".$username."' LIMIT 1";
            $dosql = mysql_query($sql, $connect);
            $result = mysql_fetch_assoc($dosql);
            
            return $result['user_id'];
      }
}
?>

You might need to alter it to fit into phpbb...ill check it up for you now.
Avatar of benwiggy

ASKER

No I'm not trying to fit into phpbb so don't worry - will try - I'm integrating a jokes portal membership system - thanks
ASKER CERTIFIED SOLUTION
Avatar of punkstar
punkstar
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks - for others browsing through, the code I ended up using and how I used it:

      $sql = "SELECT * FROM `phpbb_users` WHERE `username` = '".$userdata['username']."' LIMIT 1";
      $dosql = mysql_query($sql, $db);
      $result = mysql_fetch_assoc($dosql);
      $u_id = $result['user_id'];

      $accounttitle = "Welcome ".$userdata['username'];
      $sql = "select jokeid from jokes where userid = $u_id";
      $result = mysql_query($sql ,$db);
      $mycount = mysql_num_rows($result);

Thanks again for quick response!
no problem mate :-)