1

I am trying to display an image from a function and then while loop. Image is not getting displayed. I have made the function in functions.php and called this file in config.php. Finally config.php is called in header.php file, means functions.php file is now accessable everywhere. Every other details is coming, just image is not getting displayed

my function

function get_featured_image($p_id) {
    global $con;

    // First, get the image_id from the products table
    $featured_image_query   = mysqli_query($con, "SELECT images_id FROM products WHERE id = $p_id");
    $featured_image         = mysqli_fetch_assoc($featured_image_query);

    if ($featured_image) {
        $image_id = $featured_image['images_id'];

        // Next, fetch the image details from the images table based on the image_id
        $image_query    = mysqli_query($con, "SELECT * FROM images WHERE image_id = $image_id");
        $image_data     = mysqli_fetch_assoc($image_query);

        if ($image_data) {
            return $image_data['file_name'];
        }
    }

    return ""; // Return an empty string if no featured image is found
}

My While Loop Where i want to display the image

<?php
$productscount = 0;
while ($products = mysqli_fetch_object($product_query)) { 
$product_id = $products->id;
$featured_image = get_featured_image($product_id);   
?>
<a href="product_details.php?id=<?php echo $products->id; ?>">
 <img src="images/products/<?php echo $featured_image; ?>" class="img-fluid blur-up lazyload" alt="">
</a>
<?php } ?>

M

enter image description here

enter image description here

  • **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/32391315) – Dharman Aug 26 '23 at 23:40
  • `SELECT file_name FROM products as p join images using(image_id) WHERE p.id = ?` will remove 1 query, and fix SQL injections if put into a prepared statement and bound. – user3783243 Aug 27 '23 at 00:00
  • What does the `src` attribute come out as when you load the page? Sounds like the path for image is wrong.. use developer console to see what is being requested, and what the rendered source is. – user3783243 Aug 27 '23 at 00:02
  • I have double checked path, images are moving into images/products and getting inserted into database also. On front end, i can see only a broken image. in console there is no info as well – Ivance Edwards Aug 27 '23 at 00:07
  • by error reporting on, i have seen this error message mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, bool given in C:\xampp7.4\htdocs\gift\include\functions.php on line 68 this is 68 number line $image_data = mysqli_fetch_assoc($image_query); – Ivance Edwards Aug 27 '23 at 00:09
  • Your `mysqli` query is failing. You aren't catching mysqli errors currently so your falling into PHP error reporting. Use mysqli error reporting to get errors with your queries... (which could just be the SQL injection issue already mentioned). – user3783243 Aug 27 '23 at 00:11
  • The console likely has a 404 error for the image resource request, unless the page isn't generating at all – user3783243 Aug 27 '23 at 00:13
  • yes i have made the debugging on. Any clue where i am wrong – Ivance Edwards Aug 27 '23 at 00:19
  • just viewed the Ciew-source of the page. it is showing an image there, but the image name is not that what is inserted into database. For example, image moved in folder and inserted into database with name xyz123.jpg, whereas in viewsoruce of the page, it is showing hasdhdfhdj-250x250.jpg in – Ivance Edwards Aug 27 '23 at 00:22
  • Please disclose the $product_query variable. Based on what I can see you are trying to print all the product images from the database. How can you expect a single result in this case? – Rinsad Ahmed Aug 27 '23 at 00:37

1 Answers1

1

i think this should work. You are using wrong variables. In database you have images_id, whereas you have calling $image_id in your code. Use $images_id and it will work

if ($featured_image) {
        $image_id = $featured_image['images_id'];
        $image_query = mysqli_query($con, "SELECT * FROM images WHERE images_id = $image_id");
        $image_data = mysqli_fetch_assoc($image_query);
    
        if ($image_data) {
            return $image_data['file_name'];
        }
    }
Team Thunder
  • 91
  • 2
  • 9