Jump to content

Display images from Database


Revolutsio

Recommended Posts

My problem is i have a small database with the following headers.

file_name, uploaded_on & Game.

I have a small little upload image code and then i add the name of the game with phpMyAdmin for now..

and the following code will display the images and name of the game. but as you can see i only can get the name of the game to appear.

<div class="card-body p-2">
                        <div class="row">
                        <div class="col-4 text-center p-3 align-self-top">
                        <?php $game="SELECT file_name, Game FROM images WHERE id='1'"; ?>
                            <img class="w-100 mb-2 bg-dark" src="images/1.png">                            
                        <?php foreach ($db->query($game)as $row) {
                                echo $row['Game'];
                            } ?>                            
                        </div>
                        <div class="col-4 text-center p-3 align-self-top">
                            <img class="w-100 mb-2 bg-dark" src="images/2.png">
                            <?php $game="SELECT Game FROM images WHERE id='2'";
                            foreach ($db->query($game)as $row) {
                                echo $row['Game'];
                            } ?>
                        </div>

What I would like help with is how do i get the image from the database to show within the div statements.

 

 

Link to comment
Share on other sites

Is 'file_name' the name of the image? I am not really sure at all what you are trying to do.  All those divs!  And 2 queries.  Why 2?

And all the in-and-out of php mode.  Why?

Do you php at the top.  Get the query results.  Then begin your div(s) and loop thru the query results and output the image tag that you need.  I am guessing that your two queries are to get two specific images - one for id of 1 and one for id of 2.

 

Edited by ginerjm
Link to comment
Share on other sites

16 minutes ago, ginerjm said:

Is 'file_name' the name of the image? I am not really sure at all what you are trying to do.  All those divs!  And 2 queries.  Why 2?

And all the in-and-out of php mode.  Why?

Do you php at the top.  Get the query results.  Then begin your div(s) and loop thru the query results and output the image tag that you need.  I am guessing that your two queries are to get two specific images - one for id of 1 and one for id of 2.

 

What i am trying do is have images side by side in id order and underneath the name of the image.

 

Yes the image is called file_name

Link to comment
Share on other sites

So - as I said - do your php code at the beginning of this code. Then begin the loop

$game = "SELECT file_name, Game FROM images 
		where 1 
		order by id";
$qresults = $db->query($game);
while ($row = $qresults->fetch())
{
	echo "
		<div class='card-body p-2'>
		<img class='w-100 mb-2 bg-dark' src=\"{$row['file_name']}\">
		<center>{$row['Game']}<center>
        </div>";
          
}

The div can be setup with a width and a float to position them as wide as your body is and then they will wrap.

  • Like 1
Link to comment
Share on other sites

Thank you for you help...

I am getting an error on the fetch query

'Uncaught Error: Call to undefined method mysqli_result::fetch()'

 

ignore the above i used fetch_assoc and it works thank you again

 

Edited by Revolutsio
found a work through
Link to comment
Share on other sites

That means your query failed.  output the error message that comes from the query call.

Sorry - that was partly my fault.  I only use PDO, not mysqli and so my function name was incorrect.

BTW  pdo is a much better interface.  If you are a noob I strongly suggest learning pdo instead of mysqli

Edited by ginerjm
Link to comment
Share on other sites

would you like to see the config code or index code?

 

here is the error i get when i run the code

Quote

Fatal error: Uncaught Error: Call to undefined method mysqli_result::fetch() in G:\xampp\htdocs\Columns\Index.php:51 Stack trace: #0 {main} thrown in G:\xampp\htdocs\Columns\Index.php on line 51

 

Edited by Revolutsio
Link to comment
Share on other sites

10 minutes ago, Revolutsio said:

undefined method mysqli_result::fetch(

it means that there is no fetch() method.

if you are going to use a while(){} loop, you would use the fetch_array() method.

if you are going to use a foreach(){} loop, you can directly iterate over the rows in the result set.

Link to comment
Share on other sites

1 minute ago, mac_gyver said:

it means that there is no fetch() method.

if you are going to use a while(){} loop, you would use the fetch_array() method.

if you are going to use a foreach(){} loop, you can directly iterate over the rows in the result set.

It works with fetch_array and fetch_assoc

Link to comment
Share on other sites

!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Oswald&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="grid.css">
    <title></title>
</head>
<body>
    <?php
    include 'config.php';
    ?>
    <div class="container">
        <div class="row">
        <div class="col">
            <div class="card bg-dark mb-4">
                <div class="card-header text-center">
                    <h2 class="text-uppercase">Last Completed</h2>
                </div>
                    <div class="card-body p-2">
                        <figure>
                        <img class="w-100 text-center" src="images/1.png">
                        <figcaption class="py-2 px-3">
                            
                            <div class="figcaption-title">>observer_</div>
                            <div>2 December 2020</div>
                            
                            
                        </figcaption>
                        </figure>
                        
                    
                </div>
            </div>
        </div>
<!-- Middle Column --->
        <div class="col">
            <div class="card bg-dark mb-4">
                <div class="card-header text-center">
                    <h2 class="text-uppercase">Game Collection</h2>
                </div>
                <?php
               $game = "SELECT file_name, Game FROM images 
               where 1 
               order by id";
       $qresults = $db->query($game);
       while ($row = $qresults->fetch_array())
       {
        //    print_r($qresults);
           echo "
               <div class='card-body p-2'>
               <img class='w-100 mb-2 bg-dark' src=\"{$row['file_name']}\">
            
               </div>";
                 
       }

this is the index.php file

<?php
// Database configuration
$dbHost     = "localhost";
$dbUsername = "root";
$dbPassword = "";
$dbName     = "gameslibrary";
// Create database connection
$db = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);
// Check connection
if ($db->connect_error) {
    die("Connection failed: " . $db->connect_error);
}
?>

this is the config, do you want to see the stylesheet.

 

also i cannot get the images as i do not know how to use the src in the way you have done it

Link to comment
Share on other sites

4 minutes ago, ginerjm said:

The config file is making the connection so that is not the problem.  Nor is a stylesheet giving you an error.

Your main line code is the problem.  What line is the error message pointing to?  The line that does the fetch still?

Fatal error: Uncaught Error: Call to undefined method mysqli_result::fetch() in G:\xampp\htdocs\Columns\Index.php:51 Stack trace: #0 {main} thrown in G:\xampp\htdocs\Columns\Index.php on line 51

line 51 is the while statement

Link to comment
Share on other sites

6 minutes ago, dodgeitorelse3 said:

The where clause posted above in index.php looks wrong

Sorry iam new this, could you tell what is wrong with the where clause please

6 minutes ago, dodgeitorelse3 said:

The where clause posted above in index.php looks wrong

 

Link to comment
Share on other sites

But i have not changed the code apart from swapping the fetch clause to fetch-array as suggested above. the code i posted is the code that is in the screenshot, as you can see it is not showing the images,

this line is what i  want help on

<img class='w-100 mb-2 bg-dark' src=\"{$row['file_name']}\">

how do I show the images?

I am grateful for your help.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.