简体   繁体   中英

Displaying Image From URL Stored in SQL Databse Using PHP

I'm trying to display images from a SQL Database (using an URL in the DB) in a table.

I'm still pretty new to this type of implementation so I've been giving it my best shot and trying to do research but haven't found much on using URLs (opposed to using BLOBs or directly adding images to the Database). I definitely WANT to keep my images on a file system. Currently my code only displays the URL (obviously) but I have tried a few things..

I have tried:
-Passing $row["card_image"] to a PHP variable $image and using $image->load()
-Using "<img src="$row["card_image"]">" (I imagined since I was getting a link output still this could work...)
-I have also tried a few other solutions that I've found on Stack Overflow but I believe these have mostly been for use with BLOB data types and so they also failed to produce the output I desire (IE
"<img src="data:image/jpeg;base64,'.base64_encode($image->load())'" />"

Here is my current code:

SQL (I have condensed the fields to save space)

    CREATE TABLE cards (
    card_image VARCHAR(999)
    );

insert into cards (card_image)
values ('http://localhost/dbztc/wp-content/uploads/2016/04/1.jpg');

PHP

// Create connection
$conn = new mysqli($servername, $username, $password, $db);

if(!$conn)
{
die("connection failed");
}

//Table
echo "
        <table cellpadding=\"0\" cellspacing=\"2\" border=\"0\" width=\"100%\">
            <tr bgcolor=\"#666666\">
                <td colspan=\"5\" align=\"center\"><b><font color=\"#FFFFFF\">" . $table[0] . "</font></td>
            </tr>
            <tr>
                <td>Image</td>
                <td>Card Number</td>
                <td>Card Rarity</td>
                <td>Card Name</td>
                <td>Card Type</td>
                <td>Card Style</td>
                <td>Card Text</td>
            </tr>";

$sqlquery = "SELECT card_image, card_number, card_rarity, card_title, card_type, card_style, card_text FROM cards";
$result = mysqli_query($conn, $sqlquery);
//$image = $row["card_image"];

if (mysqli_num_rows($result) > 0) {

    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        echo "<td>" . $row["card_image"] . "</td>" . "<td>" . $row["card_number"]. "</td>" . "<td>" . $row["card_rarity"] . "</td>" . "<td>" . $row["card_title"] . "<td>" . $row["card_type"] . "</td>" . "<td>" . $row["card_style"] . "</td>" . "<td>" . $row["card_text"] . "</td></tr>";
    }
} else {
    echo "0 results";
}

echo "</table>";

mysqli_close($conn);

Instead of:

<td>" . $row["card_image"] . "</td>

Try:

$image = $row["card_image"]; // the image url

<td><img src='". $image . "'></td> 

For anyone who stumbles upon this and may be having this same problem:

For starters, I took the entire URL out of the database entry and replaced it with the Filename only.

Afterwards, I edited my code like this:

// output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        echo "<td>" . "<img src ='../images/" . $row["card_image"] . "'>" . "</td>" . "<td>" . $row["card_number"]. "</td>" . "<td>" . $row["card_rarity"] . "</td>" . "<td>" . $row["card_title"] . "<td>" . $row["card_type"] . "</td>" . "<td>" . $row["card_style"] . "</td>" . "<td>" . $row["card_text"] . "</td></tr>";
        echo "<br />";

It now displays perfectly. Just as a note, this was done on a live server opposed to local so that could maybe also have an impact? Not sure, but hopefully this can help someone!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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