0

I want to check two tables if all their contents are similar and echo yes or no Similarity. my PHP script is not work:

<?php
$path = $_SERVER['DOCUMENT_ROOT'];
include($path."db.php");
$sql = "SELECT * 
        FROM thprtins";
$sqlt = "SELECT * 
        FROM thirdparty";

$result = $conn->query($sql);
$resultt = $conn->query($sqlt);
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        $id = "" . $row["id"] . "";
        $expiry = "" . $row["expiry"] . "";
        $type = "" . $row["type"] . "";
        $financial = "" . $row["financial"] . "";
        $coid = "" . $row["coid"] . "";
        echo "$id.$expiry.$type.$financial.$coid<br>";
     }
}
if ($resultt->num_rows > 0) {
    while($rows = $resultt->fetch_assoc()) {
        $idd = "" . $rows["id"] . "";
        $expiryy = "" . $rows["expiry"] . "";
        $typee = "" . $rows["type"] . "";
        $financiall = "" . $rows["financial"] . "";
        $coidd = "" . $rows["coid"] . "";
        echo "$idd.$expiryy.$typee.$financiall.$coidd<br>";
    }
}
if ($result === $resultt) 
    echo "Two tables are similar"; 
else {
    echo "no Similarity ";
};
 ?>

I have a main table and a backup table and somewhere I just change the backup table. If the two tables are not equal, I want to do the equalization operation.

Although the content in the two tables rows is different, the output is: Two tables are similar

ramin
  • 448
  • 4
  • 15
  • Good code indentation would help us read the code and more importantly it will help **you debug your code** [Take a quick look at a coding standard](https://www.php-fig.org/psr/psr-12/) for your own benefit. You may be asked to amend this code in a few weeks/months and you will thank me in the end. – RiggsFolly Feb 17 '21 at 17:37
  • 1
    `if ($result === $resultt)` is unlikely to be what you want. What makes these 2 tables equal? – RiggsFolly Feb 17 '21 at 17:38
  • I want is not a question. Please explain what answers you are looking for. – Dharman Feb 17 '21 at 17:44
  • I have a main table and a backup table and somewhere I just change the backup table. If the two tables are not equal, I want to do the equalization operation. – ramin Feb 17 '21 at 17:48
  • What if you serialize the two databases and then check if they are the same? – Undry Feb 17 '21 at 17:51
  • @undry How does it work? – ramin Feb 17 '21 at 18:04
  • @RiggsFolly When I changed the backup table I want update the main table with the backup table – ramin Feb 17 '21 at 18:08
  • Does this answer your question? [Compare two MySQL databases](https://stackoverflow.com/questions/225772/compare-two-mysql-databases) – Don't Panic Feb 18 '21 at 09:01

1 Answers1

1

Concat all columns of each row into one field using MySQL CONCAT() and then concat all rows into one row using GROUP_CONCAT()

$sql = "SELECT GROUP_CONCAT(CONCAT(`id`,',',`expiry`,',',`type`,',',`financial`,',',`coid`) SEPARATOR ',') AS `all_data` FROM thprtins";
$sqlt = "SELECT GROUP_CONCAT(CONCAT(`id`,',',`expiry`,',',`type`,',',`financial`,',',`coid`) SEPARATOR ',') AS `all_data` FROM thirdparty";
$result = $conn->query($sql)->fetch_assoc()['all_data'];
$resultt = $conn->query($sqlt)->fetch_assoc()['all_data'];
if ($result === $resultt) 
    echo "Two tables are similar"; 
else {
    echo "no Similarity ";
}
LIGHT
  • 5,604
  • 10
  • 35
  • 78