0

I have two compare two values (say $sid1 and $sid2) and match them and find out the total number of similar and dissimilar values (I mean count the values). Please help me by giving the coding for

1) Storing the values fetched from while loop in array

2) Comparing the two arrays to count the similar and dissimilar values

**My Program**

$sql="select * from table1 where qn1='$op2'";
$ds=mysql_query($sql);

while ($r=mysql_fetch_array($ds)) {
    $sid1=$r[‘a’];
    //need correct syntax for the below
    $a[]= $sid1;
}

$sql2="select * from table2 where qn4='$op3'";
$ds2=mysql_query($sql2);

while ($r2=mysql_fetch_array($ds2)) {
    //need correct syntax for the below
    $sid2=$r2[‘a’];
    $b[]= $sid2;
}

//how to match the array and count
Array ($a[] == $b[])
Markus
  • 5,667
  • 4
  • 48
  • 64
  • whats that thing \*\*My program\*\*? –  Dec 27 '14 at 13:35
  • did you want to put it bold in SO syntax? –  Dec 27 '14 at 13:36
  • mysql_ functions are deprecated, please use [mysqli](http://php.net/manual/en/book.mysqli.php) or [PDO](http://php.net/manual/tr/book.pdo.php) instead of mysql_ functions. – salep Dec 27 '14 at 20:40

2 Answers2

0

First of all your first line is wrong. use #My Program instead of **my program** also you can store you results directly into your array. and remember that mysql() is being departed from php so you should use mysqli() functionalities. and for your propose you should use array_intersect() function. it returns an array of match items. and you can use sizeof() function for counting the number of match items, so your codes shoul be like this:

<?php
$sql="select * from table1 where qn1='$op2'";
$ds=mysqli_query($connection,$sql);
 while ($r=mysqli_fetch_assoc($ds))
{
$a[]= $r['a'];
}
$sql2="select * from table2 where qn4='$op3'";
$ds2=mysqli_query($connection,$sql2);
while ($r2=mysqli_fetch_assoc($ds2))
{
$b[]= $r2['a'];
}
$match_items=array_intersect($a, $b);
$count=sizeof($match_items);
?>
NimaNr
  • 532
  • 5
  • 20
  • I didn't know you could use # as a comment. –  Dec 27 '14 at 13:37
  • @JuanRocamonde there are three methods for commenting: // and # for single line and /* comment */ for single and multi line – NimaNr Dec 27 '14 at 13:43
0

Below program can help you.

When you SELECT * in the SQL Table then the SQL TABLE might have more than one column resulting multidimensional array like below.

array (size=200)
  0 => 
    array (size=10)
      'id' => string '18' (length=2)
      'email' => string 'abc@xyz.com' (length=11)
      'userName' => string 'abc' (length=3)
  1 => 
    array (size=10)
      'id' => string '19' (length=2)
      'email' => string 'cdf@xyz.com' (length=11)
      'userName' => string 'cdf (length=3)
  2=>
    ....
  3=>
    ....

then you have to check whether whole row of one table is matching with another table whole row. So you can achieve it by below method.

<?php
// DB Connection    
$db = new PDO( 'mysql:host=HOSTNAME;dbname=DB_NAME;charset=utf8', 'DB_USER_NAME', 'DB_PASSWORD', array( PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION ) );

//Queries
$sql_query1 = "SELECT * FROM TABLE1 WHERE `qn1` = 'ABC';";
$sql_query2 = "SELECT * FROM TABLE2 WHERE `qn2` = 'XYZ';";

$data1 = $db -> query( $sql_query1 ) -> fetchAll( PDO::FETCH_ASSOC );
$data2 = $db -> query( $sql_query2 ) -> fetchAll( PDO::FETCH_ASSOC );

$notFound = array();
$found = array();
foreach ($data1 as $key => $value1) {
    foreach ($data2 as $key => $value2) {
        if( array_diff( $value1, $value2 ) ){
            // NotFound Array
            $notFound[] = $value1;
        } else {
            // Found Array
            $found[] = $value1;
        }
    }
}

echo "Not Found data1 Elements in data2 :  " . count( $notFound ) . "\n<br/> Found data1 Elements in data2 :" . count( $found ) ;
Surya
  • 454
  • 6
  • 15