简体   繁体   中英

Cant get data using LIKE with INNER JOIN

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";
        }
        }

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

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM