简体   繁体   中英

Using PHP to find unlinked columns in two different mysql tables

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";
}

?> 

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.

Remember to add or die(mysql_error()) to the end of your mysql_* functions so it's easier to identify any issues.

Please, don't use mysql_* functions in new code . They are no longer maintained and are officially deprecated . See the red box ? Learn about prepared statements instead, and use PDO , or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM