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

  • SEARCH
  • Home
  • 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 8796677
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:40:40+00:00 2026-06-13T23:40:40+00:00

total n00b here, first post, so please be constructive! I’ve bought a book to

  • 0

total n00b here, first post, so please be constructive! I’ve bought a book to teach myself PHP / MySQL – and not one which is universally praised, unfortunately – so my progress is a bit erratic.

I have two tables which I want to link. I want to display all of the information held in table 1 (let’s call it Records), but ONLY once. I then want to check whether each item in Records has a match to a specific field in table 2 (UserTable) and if it does, display a ticked checkbox, or an empty checkbox if there’s no match.

The issues I’ve had to date are only displaying the items from Records where there’s a match, or displaying multiple instances of each item in Records where there are multiple matches. I’ve solved these with the code below – but I keep thinking there must be a better way to do this, perhaps with a single link?

Anyway, here are some excerpts from the code:

$sql = "SELECT * FROM Records";
$res = mysqli_query($mysqli,$sql);

while ($iteminfo = mysqli_fetch_array($res, MYSQLI_ASSOC)) {
$recordid2 = $iteminfo['record_id'];

I’ve omitted a section which pulls out the relevant data from Records and starts to build a table, then:

$sql2 = "SELECT COUNT(*) AS matches FROM UserTable 
where usertable.item = '$recordid2' and User_ID = '$current_user_id'";

$res2 = mysqli_query($mysqli, $sql2);
$matches = mysqli_fetch_array($res2, MYSQLI_ASSOC);
$matches2 = $matches['matches'];

if ($matches2) {
        $output = "<input type='checkbox' name='test' checked>"; } else {
        $output = "<input type='checkbox' name='test'>";

As I say, this works, but it feels a bit clumsy – I’m running a separate nested query for every item in Records, which over time could become really slow. Is there a way to run a single query for the whole which brings through all of the row info (once) and a 0 or 1 depending on whether there is a match in UserTable? I’ve tried using DISTINCT but couldn’t get it to work.

  • 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-06-13T23:40:41+00:00Added an answer on June 13, 2026 at 11:40 pm

    How about this:

    SELECT * FROM Records
    LEFT JOIN (
        SELECT usertable.item, COUNT(User_ID) AS matches FROM UserTable
        WHERE User_ID = '$current_user_id'
        GROUP BY usertable.item
    ) as UserTable ON record_id = usertable.item
    

    This fetches ALL records, and also only the matching rows for the given User ID in UserTable (change the User ID to fetch details for a different user).

    If a row in Record exists, but no matching row exists in UserTable, then “matches” will be NULL. Otherwise it should be a numeric value greater than zero.

    Just run this in PHP to handle the checkbox stuff. (it checks for matches greater than zero, and only if found, will print out the “checked” attribute)

    <input type="checkbox" name="test" <?=((int)$record['matches'] > 0) ? 'checked="checked"' : ''; ?> />
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a total C n00b trying to teach myself C off K&R. My question
i am not sure if its a total n00b question but here goes. i
Total noob here. This is my first c# attempt, its a console application that
Total n00b question here. I am using pgAdmin to attempt to administer a remote
Total n00b question here (at least to the CSS elite): Why is it that
I have very little experience with MySQL/SQL in general - total n00b really! I
Here is my total user without JOIN statement. mysql> SELECT COUNT(*) AS total ->
total n00b question here but I can't seem to find the answer searching around
Total newbie here, please bear with me. Building a very small personal app that
i'm a total n00b when it comes to PHP (and arguably, web development in

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.