Monroe Monroe - 1 year ago 51
PHP Question

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.

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");
echo $row['Category_img'];

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

Answer Source

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);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download