0

From my database my code fetch (dynamic) two values (varChar) from 2 tables and I want to find is there at least one common number between the 2 values. If at least one number is common, my code will fetch rows and display the rows values (while loop).

I am new to php and new to 'stackoverflow'. My code works fine and fetch results as I desired. The problem 1.) will it work rightly all the time? 2.) I feel my code seems to be ugly and it can be refined.

Here my code: $sql = "SELECT * FROM table1 WHERE userId = '".$_SESSION['userId']."'"; $result = $conn->query($sql); $row = $result->fetch_assoc(); $nearBy = $row['nearBy']; $area_1 = explode(',', $nearBy); //Here, for example I am getting the result - 1,4,5,9,12

 and 

 $sql = "SELECT * FROM table2 ORDER By id";
 $result = $conn->query($sql);
 while($row = $result->fetch_assoc())
 {
  $area_2 = explode(',', $row['areas']); // 9,12,24,67

 ```
 foreach ($area_1 as $value1) {
    $value1;
 foreach ($area_2 as $value2){
    $value2;
    if($value1 == $value2){
        if(!empty($result)){
 ```
       
 ```   
    $sql10 = "SELECT * FROM table2 ORDER By id";
$result10 = $conn->query($sql10);
while($row10 = $result10->fetch_assoc())
```
` 

// Finished, the idea is - I converted the first result (area_1) into an array by explode and I did the same to 'area_2'. Then I looped through both the arrays to find out at least any common number. If the result is not empty then at least one common number is available and the specific row details will be displayed. Like wise all the rows will be displayed one by one.

I again wish to remain the coders that I am very new to php coding. Thanks in advance for your upcoming helps.

My code works fine and fetch results as I desired. The problem 1.) Will it work rightly all the time? 2.) I feel my code seems to be ugly and it can be refined.

0 Answers0