PHP& MySQL:从数据库中获取图像

时间:2014-12-16 12:05:01

标签: php mysql sql mysqli explode

我上传了多张图片,并且所有图片的路径都已存储在一起。

使用explode我已将它们分开,现在我希望在旋转木马中回显它们

我正在使用的代码是:

<?php
    $con=mysqli_connect("localhost","root","","db");
    // Check connection
    if (mysqli_connect_errno())     {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    $idd = $_GET['id'];
    echo "<header id='myCarousel' class='carousel slide'>";
        /* Indicators */
        echo"<ol class='carousel-indicators'>";
            echo"<li data-target='#myCarousel' data-slide-to='0' ></li>";
            echo"<li data-target='#myCarousel' data-slide-to='1'></li>";
            echo"<li data-target='#myCarousel' data-slide-to='2'></li>";
        echo"</ol>";

        $sql = "SELECT * FROM register_office WHERE id='".$idd."'";
            $result = mysqli_query($con, $sql);
            if (mysqli_num_rows($result) > 0) 
                {
                    /* Wrapper for slides*/
                    echo"<div class='carousel-inner'>";
                        echo"<div class='item'>";
                            while($row = mysqli_fetch_assoc($result)) 
                                {
                                    $str= $row["offimage"];
                                    $array =  explode('*', $str);
                                    foreach ($array as $item) 
                                        {
                                            echo "<div class='fill'>";
                                            echo "<img src=\"http://example.com/abc/" . $item . "\" height=\"500\" width=\"2000\"/>"; 
                                            echo "</div>";
                                }   
                        echo"</div>";
                    echo"</div>";
                }
                    /*Controls*/
                    echo"<a class='left carousel-control' href='#myCarousel' data-slide='prev'>";
                        echo"<span class='icon-prev'></span>";
                    echo"</a>";
                    echo"<a class='right carousel-control' href='#myCarousel' data-slide='next'>";
                        echo"<span class='icon-next'></span>";
                    echo"</a>";
                echo"</header>";
    ?>

但它只显示一张图片。此外,当我使用下一个控件时,即使我尝试向前或向后移动,它也显示没有图像。

8 个答案:

答案 0 :(得分:1)

这是链接中的所有代码:

  1. 我在ACTIVE corousel holder中获取4个图像,在ITEM中获取4个图像。因此,有2张幻灯片,其中包含4张图片。
  2. 我使用PDO来处理它。它是如此简单的脚本编写,几乎没有错误,易于调试。
  3. 使用PDO连接到数据库:

    <?php
    $dsn = 'mysql:dbname=database_name;host=localhost';
    $user = 'username';
    $password = 'password';
    try {$mydb = new PDO($dsn, $user, $password, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING)); }
    catch (PDOException $e){echo 'Database Server Goes Down or Not Available!';}
    ?>
    

    然后,获取图像:

    <div class="item active">
                                                <?php
                                                $queryz = $mydb->prepare("SELECT * FROM table_name WHERE item=:item ORDER BY id ASC LIMIT 4,8");
                                                $queryz->execute();
                                                $data = $queryz->fetchAll();
                                                foreach ($data as $row){
                                                    $id=$row['id'];
                                                    $imagez=$row['imagez'];
                                                    ?>
                                                    <div class="col-md-3 col-sm-6 hero-feature">
                                                        <div class="thumbnail">
                                                            <?php
                                                            if ($imagez){?>
                                                            <img src="<?php echo htmlspecialchars($row['imagez']); ?>" alt="" class="img-thumbnail"/>
                                                            <?php }else{?>
                                                            <img src="../myimage/defaultimg.jpg" alt="" class="img-thumbnail"/>
                                                            <?php }?>
                                                            <div class="caption">
                                                                <h5 class="pull-left"><b><?php echo htmlspecialchars($row['prod_name']); ?></b></h5>
                                                                <h5 class="pull-right">Rp.<?php echo htmlspecialchars($row['price']); ?></h5>
                                                                <h6>discz: <b><?php echo htmlspecialchars($row['discz']); ?>%</b></h6>
                                                                <p><?php echo htmlspecialchars($row['notez']); ?></p>
                                                                <p><a href="../data.php?pcode=<?php echo htmlspecialchars($row['cdz']); ?>" class="btn btn-primary"><span class="glyphicon glyphicon-shopping-cart"></span> Beli</a> <a href="../senddata.php?pcode=<?php echo htmlspecialchars($row['cdz']); ?>" class="btn btn-default">Info &raquo;</a>
                                                                </p>
                                                            </div>
                                                        </div>
                                                    </div>
                                                <?php } ?>
                                                </div>
                                                <div class="item">
                                                <?php
                                                $queryz = $mydb->prepare("SELECT * FROM table_name WHERE item=:item ORDER BY RAND() LIMIT 4,8");
                                                $queryz->execute();
                                                $data = $queryz->fetchAll();
                                                foreach ($data as $row){
                                                    $id=$row['id'];
                                                    $imagez=$row['imagez'];
                                                    ?>
                                                    <div class="col-md-3 col-sm-6 hero-feature">
                                                        <div class="thumbnail">
                                                            <?php
                                                            if ($imagez){?>
                                                            <img src="<?php echo htmlspecialchars($row['imagez']); ?>" alt="" class="img-thumbnail"/>
                                                            <?php }else{?>
                                                            <img src="../myimage/defaultimg.jpg" alt="" class="img-thumbnail"/>
                                                            <?php }?>
                                                            <div class="caption">
                                                                <h5 class="pull-left"><b><?php echo htmlspecialchars($row['prod_name']); ?></b></h5>
                                                                <h5 class="pull-right">Rp.<?php echo htmlspecialchars($row['price']); ?></h5>
                                                                <h6>discz: <b><?php echo htmlspecialchars($row['discz']); ?>%</b></h6>
                                                                <p><?php echo htmlspecialchars($row['notez']); ?></p>
                                                                <p><a href="../data.php?pcode=<?php echo htmlspecialchars($row['cdz']); ?>" class="btn btn-primary"><span class="glyphicon glyphicon-shopping-cart"></span> Beli</a> <a href="../senddata.php?pcode=<?php echo htmlspecialchars($row['cdz']); ?>" class="btn btn-default">Info &raquo;</a>
                                                                </p>
                                                            </div>
                                                        </div>
                                                    </div>
                                                <?php } ?>
                                                </div>
    

    然后,您将专注于如何获取图像。代码如下所示:

                                        <
    
    div class="item active">
                                                <?php
                                                $queryz = $mydb->prepare("SELECT * FROM table_name WHERE item=:item ORDER BY id ASC LIMIT 4,8");
                                                $queryz->execute();
                                                $data = $queryz->fetchAll();
                                                foreach ($data as $row){
                                                    $id=$row['id'];
                                                    $imagez=$row['imagez'];
                                                    ?>
                                                    <div class="col-md-3 col-sm-6 hero-feature">
                                                        <div class="thumbnail">
                                                            <?php
                                                            if ($imagez){?>
                                                            <img src="<?php echo htmlspecialchars($row['imagez']); ?>" alt="" class="img-thumbnail"/>
                                                            <?php }else{?>
                                                            <img src="../myimage/defaultimg.jpg" alt="" class="img-thumbnail"/>
                                                            <?php }?>
                                                            <div class="caption">
                                                                <h5 class="pull-left"><b><?php echo htmlspecialchars($row['prod_name']); ?></b></h5>
                                                                <h5 class="pull-right">Rp.<?php echo htmlspecialchars($row['price']); ?></h5>
                                                                <h6>discz: <b><?php echo htmlspecialchars($row['discz']); ?>%</b></h6>
                                                                <p><?php echo htmlspecialchars($row['notez']); ?></p>
                                                                <p><a href="../data.php?pcode=<?php echo htmlspecialchars($row['cdz']); ?>" class="btn btn-primary"><span class="glyphicon glyphicon-shopping-cart"></span> Beli</a> <a href="../senddata.php?pcode=<?php echo htmlspecialchars($row['cdz']); ?>" class="btn btn-default">Info &raquo;</a>
                                                                </p>
                                                            </div>
                                                        </div>
                                                    </div>
                                                <?php } ?>
                                                </div>
    

    图像的路径存储在:

    $imagez=$row['imagez'];
    

    具有相同的目录
    ../myimage/defaultimg.jpg"
    

    注意:我很抱歉没有专家解释,但那是我在网页上写的代码。 但是,我建议您复制代码并更改所需内容然后再试一次。

答案 1 :(得分:0)

试试这会有所帮助:

使用此功能:

<img src="<?php echo $row["offimage"]; ?>"></img>

而不是

$str= $row["offimage"];

如果要显示图像。

代码:

<?php
    $con=mysqli_connect("localhost","root","","db");
    // Check connection
    if (mysqli_connect_errno())     {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    $parentid = $_GET['country'];
    $id = $_GET['fid'];

    $sql = "SELECT * FROM register_office WHERE id='".$id."'";
    $result = mysqli_query($con, $sql);
    if (mysqli_num_rows($result) > 0) 
        {
            echo "<div class='item active'>";
               do
                    {
                     $str= $row["offimage"];
                                $array =  explode('*', $str);
                                foreach ($array as $item) 
                                    {
                                        echo "<div class='fill'>";
                                        echo "<img src=\"http://example.com/abc/" . $item . "\" height=\"500\" width=\"2000\"/>"; 
                                        echo "</div>";
                            }   
                  } while($rs = mysqli_fetch_array($result));
                echo "</div>";

        }
    else 
        {
            echo "0 results";
        }
?>

答案 2 :(得分:0)

使用以下命令更改echo语句:

echo "<div class='fill' style='background-image:url(http://devoirtechnologies.in/localhealthorganization/".$item.");'></div>";

并设置此div的宽度和高度。因为div的默认高度为0(零)。

答案 3 :(得分:0)

如果只看到一个图像,则表示您的爆炸包含仅包含数组的数组 一个项目。 对于测试替换为$array = explode("*", $str); with $array = explode("*", "iamge1.png*iamge2.png"); 你应该看到两张图片。

<强>代码

<?php
    $con=mysqli_connect("localhost","root","","db");
    // Check connection
     if (mysqli_connect_errno())     {
         echo "Failed to connect to MySQL: " . mysqli_connect_error();
     }
    $idd = $_GET['id'];
    echo "<header id='myCarousel' class='carousel slide'>";
    /* Indicators */
    echo"<ol class='carousel-indicators'>";
    echo"<li data-target='#myCarousel' data-slide-to='0' ></li>";
    echo"<li data-target='#myCarousel' data-slide-to='1'></li>";
    echo"<li data-target='#myCarousel' data-slide-to='2'></li>";
    echo"</ol>";

    $sql = "SELECT * FROM register_office WHERE id='".$idd."'";
        $result = mysqli_query($con, $sql);
        if (mysqli_num_rows($result) > 0) 
        {
            /* Wrapper for slides*/
            echo "<div class='carousel-inner'>";

            while($row = mysqli_fetch_assoc($result)) 
            {
                // $str = $row["offimage"];
                $str = "iamge1.png*image2.png";

                $array =  explode('*', $str);
                foreach ($array as $item) 
                {
                    echo "<div class='item'>";
                    echo "<div class='fill'>";
                    echo "<img src=\"http://example.com/abc/" . trim($item) . "\" height=\"500\" width=\"2000\"/>"; 
                    echo "</div>";
                    echo"</div>";
                }          

                echo"</div>";
                            }
                /*Controls*/
                echo"<a class='left carousel-control' href='#myCarousel' data-slide='prev'>";
                echo "<span class='icon-prev'></span>";
                echo "</a>";
                echo "<a class='right carousel-control' href='#myCarousel' data-slide='next'>";
                echo "<span class='icon-next'></span>";
                echo "</a>";
                echo "</header>";
        }
?>

答案 4 :(得分:0)

如果你想从jquery创建一个div然后使用.html(img html标签在引号中)追加

答案 5 :(得分:0)

$sql = "SELECT * FROM register_office WHERE id='".$idd."'";

这只能从数据库中选择1项。

答案 6 :(得分:0)

     while($row = mysqli_fetch_assoc($result)) 
        {  echo"<div class='item'>";                                    
           echo "<div class='fill'>";
           echo "<img src=\"http://example.com/abc/" .$row['offimage'] . "\" height=\"500\" width=\"2000\"/>"; 
           echo "</div>"; echo"</div>";
        }   

使用它会起作用

答案 7 :(得分:0)

这里可能会有一些问题......

1。爆炸()

首先,如果您的explode()字符串在文件名之间没有星号$row1["offimage"],则您的*可能无效。在对OP的回复评论中,您已向我们提供了$row1["offimage"]内容的示例,该内容未将每个PNG文件用星号*分隔开来:

@lolka_bolka i get this outout, uploads/c376437e2a45598b2f4d89eae4f191e8.pnguploads/c376437e2a45598b2f4d89eae4f‌​191e8.png8069756be5095978123ae51fadbffe3b.pnguploads/c376437e2a45598b2f4d89eae4f‌​191e8.png8069756be5095978123ae51fadbffe3b.png04aaa414c21dc057bc594b896124068e.png 
–  user3732711 Dec 16 '14 at 12:24 

为了使您的脚本能够正常工作,我们希望看到您的$row1["offimage"]字符串看起来像这样,并用星号*分隔每个PNG文件名(使用它的目录路径):

 uploads/c376437e2a45598b2f4d89eae4f191e8.png*uploads/c376437e2a45598b2f4d89eae4f‌​191e8.png*8069756be5095978123ae51fadbffe3b.png

2。 DIV课&#39;项目&#39;

As&#34; Dan Soap&#34; &安培; &#34; Rohit开发人员&#34;在这里已经回答,要使轮播工作,你需要确保每个图像都包含在自己的item类中。

上面的代码在echo"<div class='item'>";循环之前有while

echo"<div class='item'>";应该 INSIDE while循环 - 更具体地说,它应该在你的foreach循环中 - 这样轮播中的每个图像都包含{{1 }。class。

不要忘记,您希望首先显示的item或图片(加载时)也应该有item课程,以及active课程,如此:item

3。的JavaScript

您还没有包含任何JavaScript供我们查看...如果不调用<div class="item active">函数,则不会发生任何事情,只会显示一个(静态)图像。你可能需要这样的东西:

carousel()

希望这些/部分/全部可以帮助您的旋转木马启动并运行! :)