0

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.

    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);
Thamilhan
  • 13,040
  • 5
  • 37
  • 59
Monroe
  • 177
  • 11

1 Answers1

1

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

$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.

$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

Like so

$results = mysqli_query($dbc, $get_cat_img);
if ( $result === false ) {
    echo 'query is not working: ' . mysqli_error($dbc);
    exit;
}
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • I don't think it likes inner join, i just performed to see if a simple query would work select * FROM .... and the query works, weird – Monroe May 08 '16 at 14:12
  • Difficult to help here as I dont know your database schema – RiggsFolly May 08 '16 at 14:15
  • Also make sure you have linked these tables correctly `Rest_Category.Cat_ID = Rest_Details.Cat_ID` without that it is not going to work as you expect – RiggsFolly May 08 '16 at 14:26
  • @RiggsFolly http://php.net/manual/en/function.error-reporting.php also ;-) just in case. – Funk Forty Niner May 08 '16 at 14:30
  • I was just looking that statement up. One of many holes in my knowlwdge! – RiggsFolly May 08 '16 at 14:32
  • @RiggsFolly you think they need a `while` loop here? – Funk Forty Niner May 08 '16 at 14:33
  • @RiggsFollyi think you are right about the ON... i just deleted that and it works to some extent, adding back in the error message appears, i will look into that. I should have mentioned i have error reporting right at the top of my page – Monroe May 08 '16 at 14:35
  • Ralph, I think `mysqli_report(MYSQLI_REPORT_INDEX);` is just _adding an extra error reporting to say_ You are not using an index in this query @Fred-ii- – RiggsFolly May 08 '16 at 14:39
  • @RiggsFolly *10-4 Smokey* – Funk Forty Niner May 08 '16 at 14:40
  • 1
    Ralph, well he has obviously solved it judging by the acceptance @Fred-ii- With a little help from his friends – RiggsFolly May 08 '16 at 14:40
  • found the issue, i believe. You have been great help! just now have to work out why i am seeing a ?mark instead of a the actual image. – Monroe May 08 '16 at 14:50
  • @RiggsFolly [*Right here Smokey...*](http://stackoverflow.com/questions/37100528/phpretrieving-images-from-a-database-query-issues?noredirect=1#comment61744493_37100528) as per the above comment by the OP. Pretty sure that's the clincher here ;-) – Funk Forty Niner May 08 '16 at 14:55
  • @Monroe Its far easier to store a path to an image on disk in the database than store the actual image in a blob on the database – RiggsFolly May 08 '16 at 14:59
  • @RiggsFolly oops! i am very new to this. Is that the better way to receive my goal – Monroe May 08 '16 at 15:18
  • That is the way I would personnaly approach it – RiggsFolly May 08 '16 at 15:19