How to display image from database using php

前端 未结 11 2648
闹比i
闹比i 2020-11-27 21:43

I am trying to display an image coming from the database and I was not able to display the image .but its showing like this user-1.jpg Please see my code can on

相关标签:
11条回答
  • 2020-11-27 22:09

    If you want to show images from database then first you have to insert the images in database then you can show that image on page. Follow the below code to show or display or fetch the image.

    Here I am showing image and name from database.

    Note: I am only storing the path of image in database but actual image i am storing in photo folder.

    PHP Complete Code with design: show-code.php

       <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, intial-scale=1.0"/>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <title>Show Image in PHP - Campuslife</title>
    <style>
        body{background-color: #f2f2f2; color: #333;}
        .main{box-shadow: 0 .125rem .25rem rgba(0,0,0,.075)!important; margin-top: 10px;}
        h3{background-color: #4294D1; color: #f7f7f7; padding: 15px; border-radius: 4px; box-shadow: 0 1px 6px rgba(57,73,76,0.35);}
        .img-box{margin-top: 20px;}
        .img-block{float: left; margin-right: 5px; text-align: center;}
        p{margin-top: 0;}
        img{width: 375px; min-height: 250px; margin-bottom: 10px; box-shadow: 0 .125rem .25rem rgba(0,0,0,.075)!important; border:6px solid #f7f7f7;}
    </style>
    </head>
        <body>
        <!-------------------Main Content------------------------------>
        <div class="container main">
            <h3>Showing Images from database</h3>
            <div class="img-box">
        <?php
            $host ="localhost";
            $uname = "root";
            $pwd = '123456';
            $db_name = "master";
    
            $file_path = 'photo/';
            $result = mysqli_connect($host,$uname,$pwd) or die("Could not connect to database." .mysqli_error());
            mysqli_select_db($result,$db_name) or die("Could not select the databse." .mysqli_error());
            $image_query = mysqli_query($result,"select img_name,img_path from image_table");
            while($rows = mysqli_fetch_array($image_query))
            {
                $img_name = $rows['img_name'];
                $img_src = $rows['img_path'];
            ?>
    
            <div class="img-block">
            <img src="<?php echo $img_src; ?>" alt="" title="<?php echo $img_name; ?>" width="300" height="200" class="img-responsive" />
            <p><strong><?php echo $img_name; ?></strong></p>
            </div>
    
            <?php
            }
        ?>
            </div>
        </div>
        </body>
    </body>
    </html>
    

    If you found any mistake then you can directly follow the tutorial which is i found from where. You can see the live tutorial step by step on this website.

    I hope may be you like my answer.

    Thank You

    https://www.campuslife.co.in/Php/how-to-show-image-from-database-using-php-mysql.php

    Output

    [Showing Images from Database][1]

    https://www.campuslife.co.in/Php/image/show-image1.png

    0 讨论(0)
  • 2020-11-27 22:11

    put this code to your php page.

    $sql = "SELECT * FROM userdetail";
    $result = mysqli_query("connection ", $sql);
    
    while ($row = mysqli_fetch_array($result,MYSQLI_BOTH)) {
        echo "<img src='images/".$row['image']."'>";
        echo "<p>".$row['text']. "</p>";
    }
    

    i hope this is work.

    0 讨论(0)
  • 2020-11-27 22:15
    $sqlimage  = "SELECT image FROM userdetail where `id` = $id1";
        $imageresult1 = mysqli_query($link, $sqlimage);
    
        while($rows=mysqli_fetch_assoc($imageresult1))
    {
    
        echo "<img src = 'Image/".$row['image'].'" />';
    
    
    } 
    
    0 讨论(0)
  • 2020-11-27 22:18

    For example if you use this code , you can load image from db (mysql) and display it in php5 ;)

    <?php
       $con =mysql_connect("localhost", "root" , "");
       $sdb= mysql_select_db("my_database",$con);
       $sql = "SELECT * FROM `news` WHERE 1";
       $mq = mysql_query($sql) or die ("not working query");
       $row = mysql_fetch_array($mq) or die("line 44 not working");
       $s=$row['photo'];
       echo $row['photo'];
    
       echo '<img src="'.$s.'" alt="HTML5 Icon" style="width:128px;height:128px">';
       ?>
    
    0 讨论(0)
  • 2020-11-27 22:18

    put you $image in img tag of html

    try this

    echo '<img src="your_path_to_image/'.$image.'" />';
    

    instead of

    print $image;
    

    your_path_to_image would be absolute path of your image folder like eg: /home/son/public_html/images/ or as your folder structure on server.

    Update 2 :

    if your image is resides in the same folder where this page file is exists
    you can user this

    echo '<img src="'.$image.'" />';
    
    0 讨论(0)
  • 2020-11-27 22:18

    Simply replace

    print $image;
    

    with

     echo '<img src=".$image." >';
    
    0 讨论(0)
提交回复
热议问题