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条)

报告相同问题?

悬赏问题

  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用