php - MYSQL Searching multiple tables with different columns using LIKE

46

I need to be able to do a search on multiple tables and return all parts that are similar to the searched term. My problem is that the tables have varying amounts of columns. They do have columns with similar names though. First the PHP checks the "productnumber" table to find all parts with the similar part number. Then it searches through each table to find a matching number.

Right now if a similar part is found in the Components table, it will not display any parts found from Adapters or Connectors. Instead of selecting all columns and neglecting to search all tables I'd like to search for these three columns which are found in all three tables and return results found in all tables:

part_num

image

page

if(isset($_GET['num'])) {
$num = $_GET['num'];
$numresult = mysql_query("SELECT * FROM productnumber WHERE part_num LIKE '%$num%'");

  if ($numresult) {

    while ($row = mysql_fetch_array($numresult)) {

        if ($row["title"] == "connectors") {
            $numtitle = "connectors";
            $result = mysql_query("SELECT * FROM connectors WHERE part_num LIKE '%$num%'");
        }

        if ($row["title"] == "adapters") {
            $numtitle = "adapters";
            $result = mysql_query("SELECT * FROM adapters WHERE part_num LIKE '%$num%'");
        }

        if ($row["title"] == "components") {
            $numtitle = "components";
            $result = mysql_query("SELECT * FROM components WHERE part_num LIKE '%$num%'");
        }

    }

  }

}

Any help regarding my question would be greatly appreciated :]

888

Answer

Solution:

 while ($row = mysql_fetch_array($numresult)) {

        if ($row["title"] == "connectors") {
            $numtitle = "connectors";
            $result[] = mysql_query("SELECT * FROM connectors WHERE part_num LIKE '%$num%'");
        }

        if ($row["title"] == "adapters") {
            $numtitle = "adapters";
            $result[] = mysql_query("SELECT * FROM adapters WHERE part_num LIKE '%$num%'");
        }

        if ($row["title"] == "components") {
            $numtitle = "components";
            $result[] = mysql_query("SELECT * FROM components WHERE part_num LIKE '%$num%'");
        }

    }

you have to make another loop to retrieve data from result array

People are also looking for solutions to the problem: php - parse_str randomly adding semicolons to keys

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.