【问题标题】:Loading images to Bootstrap carousel from a MySQL database using PDO (fetchall problem)使用 PDO 将图像从 MySQL 数据库加载到 Bootstrap 轮播(fetchall 问题)
【发布时间】:2020-07-17 11:47:47
【问题描述】:

我正在使用 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) 以将图像从数据库加载到引导轮播时。没用的

<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>

【问题讨论】:

  • special 表中有多少行?你能检查(使用 PHPMyAdmin 之类的东西)imageurl 的值是多少吗?
  • @NigelRen 7 行,值为:- Efexor.jpg 路径上传/17.jpg 上传/17.jpg 上传/19.jpg 上传/18.jpg 上传/18.jpg
  • 任何帮助.....!?
  • @MrObscure 您的图片路径的完整 URL 是什么?
  • @ElmanHuseynov !!!天哪,我太愚蠢了,它不在看! [imageurl] 路径我将它移动到正确的路径并且它工作!感谢分配

标签: php mysql bootstrap-4


【解决方案1】:

试试这样:

<!-- The slideshow -->
  <div class="carousel-inner">
  <?
    $i = 0;
   $full_url = 'YOUR URL AND PATH TO IMAGE FOLDER HERE';
    foreach($result as $row){
        $actives = '';
    if($i == 0){
        $actives ='active';
    }
    ?>
    <div class="carousel-item  <?= $actives;?>">
      <img src="<?= $full_url . $row['imageurl']?>">
    </div>
    <? echo $full_url . $row['imageurl'];?>
    <? $i++; }?>
  </div>

【讨论】:

    猜你喜欢
    • 2017-05-12
    • 1970-01-01
    • 1970-01-01
    • 2016-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多