使用 PDO 将图像从 MySQL 数据库加载到 Bootstrap carouse

我正在使用 PDO 将图像从数据库插入到 Bootstrap 轮播。我的意思是(加载图像路径),但问题是当我使用fetchall();或fetch获取所有行值时,如下所示:


require_once '..\Config.php';


$dbCon = "mysql:host=$host;dbname=$db_name";

$conn = new PDO($dbCon, $username, $password);

$getquery = $conn->prepare('SELECT (imageurl) FROM special');

$getquery->execute();

$result = $getquery->fetchall();

echo $result['imageurl'];

输出是:Efexor.jpg path upload/17.jpg upload/17.jpg upload/19.jpg upload/18.jpg upload/18.jpg*这是 imageurl 列中的所有行,所以当我循环foreach( $result) 加载从数据库到 Bootstrap 轮播的图像。它不起作用


<div id="demo" class="carousel slide" data-ride="carousel">


  <!-- Indicators -->

  <ul class="carousel-indicators">

  <?

    $i = 0;

    foreach($result as $row){

        $actives = '';

    if($i == 0){

        $actives ='active';

    }

    ?>

    <li data-target="#demo" data-slide-to="<?= $i; ?>" class="<?= $actives;?>"></li>

    <? $i++ ; }?>

  </ul>


  <!-- The slideshow -->

  <div class="carousel-inner">

  <?

    $i = 0;

    foreach($result as $row){

        $actives = '';

    if($i == 0){

        $actives ='active';

    }

    ?>

    <div class="carousel-item  <?= $actives;?>">

      <img src="<?= $row['imageurl']?>">

    </div>

    <? echo $row['imageurl'];?>

    <? $i++; }?>

  </div>


  <!-- Left and right controls -->

  <a class="carousel-control-prev" href="#demo" data-slide="prev">

    <span class="carousel-control-prev-icon"></span>

  </a>

  <a class="carousel-control-next" href="#demo" data-slide="next">

    <span class="carousel-control-next-icon"></span>

  </a>

</div>


哔哔one
浏览 83回答 1
1回答

倚天杖

试试这样:<!-- The slideshow -->&nbsp; <div class="carousel-inner">&nbsp; <?&nbsp; &nbsp; $i = 0;&nbsp; &nbsp;$full_url = 'YOUR URL AND PATH TO IMAGE FOLDER HERE';&nbsp; &nbsp; foreach($result as $row){&nbsp; &nbsp; &nbsp; &nbsp; $actives = '';&nbsp; &nbsp; if($i == 0){&nbsp; &nbsp; &nbsp; &nbsp; $actives ='active';&nbsp; &nbsp; }&nbsp; &nbsp; ?>&nbsp; &nbsp; <div class="carousel-item&nbsp; <?= $actives;?>">&nbsp; &nbsp; &nbsp; <img src="<?= $full_url . $row['imageurl']?>">&nbsp; &nbsp; </div>&nbsp; &nbsp; <? echo $full_url . $row['imageurl'];?>&nbsp; &nbsp; <? $i++; }?>&nbsp; </div>
打开App,查看更多内容
随时随地看视频慕课网APP