Images as links in MySQL database?

2019-06-13 07:01发布

问题:

I want to store image links into a database and then echo them onto a page.... Is this possible and how do I do it? (echo the actuall images and not the image link)

As you can see my code echo's out: The $link, $description and $title. I want to add another element which echos $imagelink.

echo "
<div class=\"pagination\" style=\"display:inline\"><ul style=\"background-color:#\"><li><div   class=\"span3_search\"><h2><a href='$link'><b>$title</b></a></h2><br><img id=\"result_img\"   src=\"img/lvmo.png\" /><br />
$description<br />
<a href='$link'>$link<br /><br /></a><p></div></li></ul></div>
";

回答1:

Short answer

Simply echo out the URL of the image in the src attribute and the image will be displayed.

<img id="result_img" src="<?php echo $imagelink; ?>" />

Worked Solution:

Table Design:

images

| id | title      | link                  | imagelink    | description |
------------------------------------------------------------------------
| 1  | Some Title | http://www.google.com | myimage1.png | Some text   |
------------------------------------------------------------------------
| 2  | My Title   | http://www.yahoo.com  | myimage2.png | Some text   |         

SQL Query

SELECT title, link, imagelink, description FROM images WHERE id = ?

PHP

<?php

$stmt = $mysqli->prepare("SELECT title, link, imagelink, description FROM images WHERE ID = ?");
$stmt->bind_param("i", $ID);
$stmt->execute();
$stmt->bind_result($title,$link,$imagelink,$description);
$stmt->fetch();

?>
<div class="pagination" style="display:inline">
  <ul style="background-color:#">
    <li><div   class="span3_search">
    <h2><a href='<?php echo $link; ?>'><b><?php echo $title; ?></b></a></h2>
    <br />
    <img id="result_img" src="<?php echo $link; ?>" />
    <br />
    <?php echo $description; ?>
    <br />
    <a href='<?php echo $link; ?>'><?php echo $link; ?><br /><br /></a>
    <p></div>
    </li>
  </ul>
 </div>


回答2:

Storing links to images in a database is easy. The slightly harder part is uploading the images and he hardest part is ensuring that the uploaded file and the database record are in synch. What you store is typically the file name you saved the image into.

Edit: You can also store the image file in a blob in the database. This would need a program that takes a database key and return a binary stream in a response with the correct mime type. This is a lot more work and most databases are more expensive and slower than the file system.



标签: php mysql echo