-3

I have 2 tables in my db, departments and company_com. What I am trying to do is, if a posted value in php is run in a query, check the first table for a value and if found, query the second table with that value.

So, for example, posted value = demo, I run select query to store the value in a variable and then use that variable in query on company_com. I know how to query tables but not sure how to store the value in a variable in php. Thanks

BTW I am using MySql and not pdo or mysqli.

user1532468
  • 1,723
  • 8
  • 41
  • 80

1 Answers1

1
$result1 = mysqli_query($conn,"SELECT data1 FROM firsttable");
while($row = mysqli_fetch_array($result1)){
    $result2 = mysqli_query($conn, "SELECT data2 FROM secondtable WHERE data2 = '".$row[0]."'");
    while($row2 = mysqli_fetch_array($result2)){
        echo $row2[0];
    }
}
  • $result1 is first query.
  • first while loop goes through first query
  • $result2 is second query which takes WHERE data from first query's results.
  • second loop goes through second query and echos data from second query which we compared to data from first query.

    Hope that made any sense.

Siim
  • 180
  • 7