Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
0

Is there a better way of Comparing Two Rows from the same table

Community Expert ,
Nov 27, 2012 Nov 27, 2012

Copy link to clipboard

Copied

Here's the problem. I have a database which has two unique keys. One is page id, the other is user ID. Then there are 60 columns of variables that need to be compared. For example if the rows looked like this:

id   | user id | page id  | var1  | var2  | var3   |  var4  | --- | var60

============================================================================

5   |    20   |   null   |   A   |   B   | null   |    D   | --- |   DD

36   |   null  |   125    |  null |   B   |   C    |  null  | --- |   DD   

I would like to echo 'true'

But if var2 was a null and var60 was a null (so there were no common columns) I would echo 'false'

I can do this with a huge group of if statements but I would like to figure out how to get an if statement that simply said:

if (row id 5 has any columns that match row id 36){

    {echo 'true'; }

   else

    echo 'false';

Isolating each row is easy, this is how I did it. I have a currentUserRS (record set) that returns the user ID for any logged in user from the session variable, and a pageRS (current page RS) that returns the page id. There is a table that sets variables for each user and for each page in the table docSecurity. I just need to see if any of the 60 variables match.

This code reliably identifies the page ID and the current user ID. I'm hoping to get the results I need without writing 60 if statements and then checking to see if any of them are t

rue. BTW, each of the variable fields is unique, for example, var1 is always A or null. No other values are permitted in the table.

Here's my code that grabs the user and page id's.

$checkVal = $row_currentUserRS['id'];

$pageChkVal =  $row_pageRS ['id'];

$result = mysql_query("select * FROM docSecurity WHERE securityID = $checkVal");

$result2 = mysql_query("select * FROM docSecurity WHERE pageID = $pageChkVal");

$row = mysql_fetch_array($result );

$row2 = mysql_fetch_array($result2);

    echo 'User Security ID: ', $row['id'], ' / Page Security ID', $row2['id'];

TOPICS
Server side applications

Views

432
Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 27, 2012 Nov 27, 2012

Copy link to clipboard

Copied

LATEST

The recordset is just an array and the columns can be referenced by name or position. You just need to use a loop that references the columns by their numeric index and compares row 1 with row 2.  As soon as it finds a match you can assume TRUE and drop out of the loop. If it loops through without a match, it's FALSE.

You probably know php much better than me, so here's some psuedocode:

column_match = FALSE

For x = 0 to Len(row1[])-1

    If row1 = row2 then

        column_match = TRUE

        break;

    End If

Next x

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines