Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 259633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T22:16:49+00:00 2026-05-11T22:16:49+00:00

(Using MySQL and PHP) I have a search form that will allow my users

  • 0

(Using MySQL and PHP)

I have a search form that will allow my users to type in a string, and search that string on a particular criteria.

My problem is that a user needs to be able to search for information that is “spread” across multiple tables. For example:

-Table “users” contains fname, lname, role, username (PK)

-Table “resident assistant” contains username (FK to users), building, room, region

-Table “area coordinator” contains username (FK to users), office_bldg, office_num

And I am allowing my users to search by First Name, Last Name, Building, Region, Office # – So I will need to show results that span across multiple tables (i.e. matching records from “users” and “resident assistant”)

I’ve been experimenting with Joins and Unions, but haven’t quite gotten anything useful. I am looking for the most “Universal” SQL statement to handle any search, if that’s possible.

Right now, the only way I can think of doing these searches is by a lot of processing in the PHP, for example, to find a First Name, have a query that returns username, role from “users”, and then have a bunch of if statements saying, “if role is this, then search this table where username equals that…”

Is there a better way to do this?


Vinko-

I am actually not getting an error, the query (with multiple joins) is just returning 0 rows.

Here is an example query that I am using:

select u.fname, u.lname, u.role, u.username, r.building, r.room, r.region, 
a.office, a.office_num
from 
users u 
join `ra_ca` r on (u.username = r.username) 
join `area_coord` a  on (u.username = a.username)
where
u.username = 'behrk2' and r.region = '4'

And here are my table structures:

CREATE TABLE `users` (
 `fname` varchar(50) NOT NULL,
 `lname` varchar(50) NOT NULL,
 `role` varchar(75) NOT NULL,
 `extension` int(4) default '6226',
 `username` varchar(25) NOT NULL,
 `password` varchar(75) NOT NULL,
 `new_pass` varchar(5) default NULL,
 PRIMARY KEY  (`username`),
 KEY `role` (`role`),
 CONSTRAINT `users_ibfk_1` FOREIGN KEY (`role`) REFERENCES `role` (`role`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8

CREATE TABLE `ra_ca` (
 `username` varchar(25) NOT NULL,
 `region` tinyint(4) NOT NULL,
 `building` varchar(75) NOT NULL,
 `room` varchar(10) NOT NULL,
 PRIMARY KEY  (`username`),
 KEY `region` (`region`),
 KEY `building` (`building`),
 CONSTRAINT `ra_ca_ibfk_9` FOREIGN KEY (`building`) REFERENCES `building` (`building`) ON DELETE CASCADE ON UPDATE CASCADE,
 CONSTRAINT `ra_ca_ibfk_7` FOREIGN KEY (`username`) REFERENCES `users` (`username`) ON DELETE CASCADE ON UPDATE CASCADE,
 CONSTRAINT `ra_ca_ibfk_8` FOREIGN KEY (`region`) REFERENCES `region` (`region`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8

CREATE TABLE `area_coord` (
 `username` varchar(25) NOT NULL,
 `region` tinyint(4) NOT NULL,
 `building` varchar(75) NOT NULL,
 `room` varchar(10) NOT NULL,
 `office` varchar(75) NOT NULL,
 `office_num` varchar(10) NOT NULL,
 PRIMARY KEY  (`username`),
 KEY `region` (`region`),
 KEY `building` (`building`),
 CONSTRAINT `area_coord_ibfk_9` FOREIGN KEY (`building`) REFERENCES `building` (`building`) ON DELETE CASCADE ON UPDATE CASCADE,
 CONSTRAINT `area_coord_ibfk_7` FOREIGN KEY (`username`) REFERENCES `users` (`username`) ON DELETE CASCADE ON UPDATE CASCADE,
 CONSTRAINT `area_coord_ibfk_8` FOREIGN KEY (`region`) REFERENCES `region` (`region`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8

And I do have values in the DB…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T22:16:50+00:00Added an answer on May 11, 2026 at 10:16 pm

    With something like the following you’ll only need to build in code the where clause. This is, the last line of the query.

    select u.fname, u.lname, u.role, u.username, r.building, r.room, r.region, 
    a.office_bldg, a.office_num
    from 
    users u 
    join `resident assistant` r on (u.username = r.username) 
    join `area coordinator` a  on (u.username = a.username)
    where
    u.username = 'foo' and r.region = 'China'
    

    EDIT:

    It seems to me that you want all results no matter if there are values in all joined tables. So try left joins instead of inner joins. Try reading up on SQL to know WHAT are these queries doing.

    select u.fname, u.lname, u.role, u.username, r.building, r.room, r.region, 
    a.office_bldg, a.office_num
    from 
    users u 
    left join `resident assistant` r on (u.username = r.username) 
    left join `area coordinator` a  on (u.username = a.username)
    where
    u.username = 'foo' and r.region = 'China'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 124k
  • Answers 124k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Are you running on OS 3.0? I saw the same… May 12, 2026 at 1:19 am
  • Editorial Team
    Editorial Team added an answer It looks like you need to register Apache::Session::Memcached with Apache::Session::Wrapper,… May 12, 2026 at 1:19 am
  • Editorial Team
    Editorial Team added an answer Use DATENAME or DATEPART: SELECT DATENAME(dw,GETDATE()) -- Friday SELECT DATEPART(dw,GETDATE())… May 12, 2026 at 1:19 am

Related Questions

Here is the scenario 1. I have a table called items, inside the table
In a lot of databases I seem to be working on these days I
I am trying to track what users are searching for on my site (from
I am writing a PHP/MySQL program and I would like to know how to

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.