donglinxin8765 2014-04-27 01:06
浏览 121
已采纳

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.

  • 写回答

4条回答 默认 最新

  • douyongwan5946 2014-04-27 19:50
    关注

    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";
            }
        }
    }
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?