MYSQL,JOIN,AS,LIKE

I have very limited to zero knowledge about SQL or DBs, so apologies in advance.

I am attempting to make a table like structure within a webpage, a column for meal name, and a column for ingredients - it is also searchable via a button.

EDIT:

Thanks for all the replies, they have been most helpful. The error no longer happens, however now I get no results from the database, but no error.

include("inc/dbConn.php"); /////$db_conx

if(isset($_POST["search"])){   
    $searchq = $_POST["search"];
    $sql ="
           SELECT main_meal.name, ingred.name
           FROM main_meal
           JOIN meal_ingred on meal_ingred.meal_id = main_meal.id
           JOIN ingred ON ingred.id = meal_ingred.ingred_id
           WHERE ingred.name LIKE '%$searchq%'";
    $results = $db_conx->query($sql);   
    var_dump($results);
    if($results->num_rows){

        while ($row =$results->fetch_object()){
            echo "{$row->name} ({$row->name})<br>";

        }     
    }    
}

For clarification here is my structure:

(Sorry for external link, I dont have enough reputation to post images.)

http://i30.photobucket.com/albums/c340/Tyrage/Untitled-2_zpsc48126b3.png

The $results dumps:

object(mysqli_result)#2 (5) { ["current_field"]=> int(0) ["field_count"]=> int(2) ["lengths"]=> NULL ["num_rows"]=> int(0) ["type"]=> int(0) }

It is quite obvious I don't have a clue what I am doing, so any help is appreciated.

Thanks.

You could try this?

SELECT * 
FROM main_meal
JOIN meal_ingred ON meal_ingred.meal_id = main_meal.id
JOIN ingred ON meal_ingred.meal_id = main_meal.id
WHERE ingred.ingredName LIKE  '%bread%'
LIMIT 0 , 30

with the php as follow:

<?php
if(isset($_POST["search"])){
    $searchq = $_POST["search"];
    $sql ="
        SELECT * 
FROM main_meal
JOIN meal_ingred ON meal_ingred.meal_id = main_meal.id
JOIN ingred ON meal_ingred.meal_id = main_meal.id
WHERE ingred.ingredName LIKE  '%$searchq%'
LIMIT 0 , 30
          ";
    //$results = $db_conx->query($sql);
    if (!$results = $db_conx->query($sql)) {
        printf("Error: %s
", $db_conx->error);
    }
    $count = $results->num_rows;

    if($count > 0){
        while($row=$results->fetch_assoc()){
            echo $row['ingredName'];
            echo $row['name'] . "<br />";
            $output="ff";
        }
    }
}

?>

Line numbers would help, to see if line 15 is the assignment to $results, or if line 15 is the $results->num_rows

I'm also assuming you have a $db_conx->connect command somewhere in your code, to open the database connection before running the query?

Your SQL query is invalid: there is no such column as ingred.name available in the WHERE clause, because you have renamed that table as b.

There is no reason to rename your tables (using AS ...) in this query. Leave that clause out. Then add a join condition (e.g, ... JOIN ingred ON (...) — omitting this will make the query return every row from main_meal.

Additionally, there is probably a SQL injection vulnerability in your code, because you are interpolating the contents of the (likely unfiltered) $searchq variable into the query. Use a query placeholder for the argument to LIKE.

you're lacking a relation table here, which will store connection between meals and ingredients.

also you had error in your query because at first you've tried to use a/b naming correctly, to name a column, and then you user it to name a table.

IMHO you should do something like this (with relation table added):

if(isset($_POST["search"])){   
    $searchq = $_POST["search"];
    $sql ="
           SELECT main_meal.name AS a, ingred.name as b
           FROM main_meal 
           JOIN rel_meal_ingred on rel_meal_ingred.meal_id = main_meal.id
           JOIN ingred ON ingred.id =  rel_meal_ingred.ingred_id
           where ingred.name LIKE '%$searchq%'";

    $results = $db_conx->query($sql);   

       if($results->num_rows){ //////line 15
       while ($row =$results->fetch_object()){
       echo "{$row->a} ({$row->b})<br>";

       }       
       }    
}