SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot
    Join Date
    Jan 2009
    Posts
    144
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Ayn better way to do that?

    I need to display all users from a company and users details (fullname, age, position).

    I am wondering if the below method is a correct way to display 15 employees per page. In this method on each page 16 queries are executed and this is what I am worried about. Is that far too much or is it still ok?

    table employees
    id | userid | companyid | position

    table users
    userid | firstname | lastname | age | email | password ...
    PHP Code:
    $employees=mysql_query("SELECT userid FROM employees WHERE companyid='$companyid' LIMIT $paginationLimit ");
    while(
    $rowemployees=mysql_fetch_assoc($employees){
      
    $userid=$rowemployees[userid];
      
    $employeeDetails=mysql_query("SELECT * FROM users WHERE userid='$userid'");
      while(
    $rowEmployeeDetails=mysql_fetch_assoc($employeeDetails){
        
    $firstname=$rowEmployeeDetails[firstname];
        
    $lastname=$rowEmployeeDetails[lastname];
        
    $age=$rowEmployeeDetails[age];
        
    $position=$rowEmployeeDetails[position];
      }


  2. #2
    SitePoint Zealot
    Join Date
    Jan 2009
    Posts
    144
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I missed something. I need to get from table employees also position. Also in subquery there is no while. So the query is:
    PHP Code:
    $employees=mysql_query("SELECT userid, position FROM employees WHERE companyid='$companyid' LIMIT $paginationLimit ");
    while(
    $rowemployees=mysql_fetch_assoc($employees){
      
    $userid=$rowemployees[userid];
      
    $position[]=$rowemployees[position];
      
    $employeeDetails=mysql_query("SELECT * FROM users WHERE userid='$userid'");
      
    $rowEmployeeDetails=mysql_fetch_assoc($employeeDetails)
        
    $firstname[]=$rowEmployeeDetails[firstname];
        
    $lastname[]=$rowEmployeeDetails[lastname];
        
    $age[]=$rowEmployeeDetails[age];


  3. #3
    SitePoint Wizard
    Join Date
    Jul 2003
    Location
    Kent
    Posts
    1,921
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You should be using a join to get the info from the users table!!!

    SELECT e.userid, e.position, u.firstname, u.lastname, u.age
    FROM employees as e
    JOIN users as u
    ON e.userid = u.userid
    WHERE e.companyid = '$companyid' LIMIT $paginationLimit;

  4. #4
    SitePoint Wizard guelphdad's Avatar
    Join Date
    Oct 2003
    Location
    St. Catharines, ON Canada
    Posts
    1,708
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    please use more descriptive thread titles so folks know what the thread is about.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •