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 9087765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:46:16+00:00 2026-06-16T21:46:16+00:00

So I have two tables, one called ‘questions’ and one called ‘tests’. Each row

  • 0

So I have two tables, one called ‘questions’ and one called ‘tests’. Each row in ‘questions’ has the columns ‘id’,’test’ and several other columns not relevant to this question. The ‘tests’ table has the columns ‘id’,’name’ and several others not relevant.

Basically, on my website, every time someone creates a test, a new row is created in ‘tests’ and its given an ‘id’. From there, every question the user makes, creates a new row in the ‘questions’ table, each question is given an ‘id’ as well as ‘test’ column having the same number as the ‘id’ from the ‘tests’ table to which it belongs.

Yesterday we mistakenly deleted several rows from the ‘tests’ table, however all the questions are still in the ‘questions’ table, meaning we just need to find which rows in the ‘questions’ table have a ‘test’ value that isn’t linked to any ‘id’ in the ‘tests’ table, so this is the .php script we came up with:

<?php
$conn = mysql_connect ('localhost', 'hidden',  'hidden') or die ('Error connecting');
if (!$conn) {
    die('Could not connect: ' . mysql_error());
}
mysql_select_db('hidden', $conn);
$sql = "SELECT * FROM questions";
$result = mysql_query($sql,$conn);

while ($row = mysql_fetch_array($result)) {
    $test = $row['test'];
    $questionID = $row['id'];
    $sql2 = "SELECT * FROM tests where id = $test";
    $result2 = mysql_query($sql2, $conn);
    $row2 = mysql_fetch_array($result2);
    $num_rows = mysql_num_rows($result2);
    if ($num_rows == 0) {
        echo "Unlinked question: $questionID (test $test) <br/>";
    }
}
?>

However, it doesn’t echo any “Unlinked question.. ” which would help us identify which tests we have lost and need to recreate, giving them the correct ‘id’ to link to the ‘questions’ table ‘test’ column. Is there another way to fix this problem? And yes, we have do backup the tables, but unfortunately we only do it every other week, and most of the deleted tests were recent.

Update:

$sql = "
SELECT q.id AS QuestionID
FROM questions q
LEFT JOIN tests t
ON  (q.test = t.id)
WHERE q.test IS NOT NULL AND t.id IS NULL";

$result = mysql_query($sql,$conn) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
$questionID = $row['QuestionID'];
$testID = $row['test'];
echo $row;
echo "The question with the id: $questionID is not linked to the test: $testID";
}

?> 
  • 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-16T21:46:17+00:00Added an answer on June 16, 2026 at 9:46 pm

    Why wouldn’t you do this in a single SQL statement??

    SELECT
        q.id AS QuestionID,
        CASE WHEN q.test IS NOT NULL AND t.id IS NULL THEN 1 ELSE 0 END AS UnlinkedQuestion
    FROM
        questions q
    LEFT JOIN
        tests t
        ON  (q.test = t.id)
    

    OR, if you only want to return unlinked question id, simply move the logic into the WHERE clause:

    SELECT
        q.id AS QuestionID
    FROM
        questions q
    LEFT JOIN
        tests t
        ON  (q.test = t.id)
    WHERE
        q.test IS NOT NULL AND
        t.id IS NULL
    

    NOTE:

    I would highly recommend that you add foreign key constraints to your question table on the test id, so that you can prevent the same error with accidentally deleting records from your test table.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables, one called Events and one called Documents. Each table has
I have two tables: one called Projects, and another called Documents (projects has many
I have two tables, one called countries and one called country. Countries has both
I have two tables. One of them called conversations has the following data. ConversationID
I have two tables: one called styles, which has 5 attributes (plus id and
I have two tables one called fs_note the other called dumy_fs_note I created after
i have two tables one is called addons and holds information about different addons,
I have two tables, one called cart and one called items. Now I want
So i'm having this problem. I have two tables (Oracle), one is called Destination
I have two tables one called details and the other called c_details. Both tables

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.