0

I'm working on a simple search using php. I'm trying to display data from 2 tables using INNER JOIN and LIKE. Using the keyword I need to check if the keyword exist on one of the tables. my problem is its not showing any data.

it also shows a warning Notice: Trying to get property 'num_rows' of non-object

Thanks.

SAMPLE CODE

$keyword = $_POST['keyword'];

        if($keyword != ""){

            $sql = "SELECT * FROM history_search INNER JOIN history_subs ON history_search.keyword = history_subs.keyword WHERE keyword LIKE '%$keyword%'";

        $result = $conn->query($sql);

        if ($result->num_rows > 0) {
            // output data of each row
            while($row = $result->fetch_assoc()) {
                echo $row['keyword'];
            }
        } else {
            echo "0 results";
        }
        }

2 Answers2

1

Column name keyword is ambiguous as it appears in both tables

Try:

SELECT * 
FROM history_search 
INNER JOIN history_subs 
    ON history_search.keyword = history_subs.keyword 
WHERE history_search.keyword LIKE '%$keyword%' -- Added a table reference here
JohnHC
  • 10,935
  • 1
  • 24
  • 40
0

Maybe a silly question. But if you are using a WHERE clause with a LIKE then the Keyword field MAY only contain that keyword in the whole field. However on your join you are join as an exact match for the same field.

What I am getting at is if you had 2 keyword fields that contained comments for example, then why are they joining on an exact match? If you remove the where clause are you then returning any results?

Dev
  • 16
  • 1