[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 211
  • Last Modified:

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 }
0
benwiggy
Asked:
benwiggy
  • 3
  • 2
1 Solution
 
punkstarCommented:
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.
0
 
benwiggyAuthor Commented:
No I'm not trying to fit into phpbb so don't worry - will try - I'm integrating a jokes portal membership system - thanks
0
 
punkstarCommented:
I have just checked the phpbb users table, and that is the correct function...so now, to use the function simply..

<?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'];
      }
}

//this is username variable
$username = "punkstar";
$user_id = username2userid($username);
?>

...also this script takes it for granted that you are using MySQL.
0
 
benwiggyAuthor Commented:
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!
0
 
punkstarCommented:
no problem mate :-)
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now