简体   繁体   English

PHP:从数据库查询中检索图像

[英]PHP:Retrieving images from a database query issues

I have read many posts but i cannot find my answer. 我读了很多帖子,但找不到答案。 I am developing a food order/delivery website, which has many food cuisine categories, African, Alcohol, American... Each category is meant to have a different header image. 我正在开发一个食品订购/配送网站,其中包含许多食品类别,包括非洲,酒精,美洲...每个类别都具有不同的标题图像。 So if the admin creates a new restaurant, when they select the restaurant cuisine, the correct header image will automatically display on the main websites products page for that said restaurant. 因此,如果管理员创建了一个新餐厅,则当他们选择餐厅美食时,正确的标题图片将自动在该餐厅的主要网站产品页面上显示。

I have manually inputted the images into the database already, now i am trying to retrieve the database, my or die statement prints that it is not, but i have no error messages, which is confusing me. 我已经将图像手动输入到数据库中,现在我正在尝试检索数据库,我的或die语句显示不是,但是我没有错误消息,这使我感到困惑。

    mysqli_report(MYSQLI_REPORT_INDEX);
    if (isset($_GET['rest_id'])) {

        $Rest = $_GET['rest_id'];

        $get_cat_img = "SELECT Cuisine_category
        FROM Rest_Category,Category_img
        INNER JOIN Rest_Details
        ON Rest_Category.Cat_ID = Rest_Details.Cat_ID
        WHERE Rest_Details.Cat_ID='$Rest'";

        $results = mysqli_query($dbc, $get_cat_img) or die("query is not working");
        $row=mysqli_fetch_array($results) or die ("q not working");
        $img=$row['Category_img'];
        echo $row['Category_img'];

        echo '<img src="'.$img.'" alt="background" style="width:100%;height:300px">';           
    }
    mysqli_close($dbc);

I think you may have just put a column name in the wrong place in your query. 我认为您可能只是将列名放在查询的错误位置。

If Category_img is a column name in the Rest_Category table, this is what you want to do 如果Category_imgRest_Category表中的列名,这就是您要执行的操作

$get_cat_img = "SELECT Cuisine_category,Category_img
                FROM Rest_Category
                  INNER JOIN Rest_Details ON Rest_Category.Cat_ID = Rest_Details.Cat_ID
                WHERE Rest_Details.Cat_ID='$Rest'";

You can also shorten things a bit by using Alias's, it often makes the SQL code easier to read when it get past the very simple query. 您还可以通过使用Alias的方法来缩短一些时间,这通常会使经过简单查询的SQL代码更易于阅读。

$get_cat_img = "SELECT rc.Cuisine_category,rc.Category_img
                FROM Rest_Category rc
                  INNER JOIN Rest_Details rd ON rc.Cat_ID = rd.Cat_ID
                WHERE rd.Cat_ID='$Rest'";

Also modify your error reporting to actually report the real MYSQL error, it is much more usful that any message you can some up with 还可以修改错误报告以实际报告真正的MYSQL错误,您可以参考的任何消息都更加有用。

Like so 像这样

$results = mysqli_query($dbc, $get_cat_img);
if ( $result === false ) {
    echo 'query is not working: ' . mysqli_error($dbc);
    exit;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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