douwan2664 2015-04-21 17:05
浏览 32
已采纳

带有SQL的图像轮播[关闭]

I wanted to start an image carousel where the images are paths from a database.

I am not sure why this code is not working.

<!DOCTYPE HTML>
<html lang="sv-SE"/>
<head>
<meta charset="UTF-8"/>
<title>Images</title>
</head>
<body>
<?php

$mysqli = new mysqli('localhost', 'root', '', 'webshop');


$sql = "SELECT  productimage FROM productlist LIMIT 5";
$result = $mysqli->query($sql);


while($myRow = $result->fetch_array())
    {   

        $index = $_GET['start'];

        //Start crausel when clicking prev
        if ($index > 0) {
            echo '<a href="index.php?start=' . ($index - 1) . '"> prev </a> ';
        } else {
            echo '<a href="index.php?start=' . (count($myRow) - 1) . '"> prev </a> ';
        }


        //Display 3 pictures on crausel
        $show_img = 3;
        $num_img = 0;
        for($i=$_GET['start']; $i<count($myRow) && $num_img < $show_img; $i++) {
            $num_img++;
            echo "<img src=".$myRow[$i]."/>
";
        }

        for($i=$num_img; $i<$show_img;$i++) {
            echo "<img src=".(count($myRow) - 1)."/>
"; 
        }

        //Start crausel when clicking prev
        if ($index < count($myRow) - 1) {
            echo '<a href="index.php?start=' . ($index + 1) . '"> next </a> ';
        } else {
            echo '<a href="index.php?start=0"> next </a>';
        }
    }
?>
</body>
</html>

The error is:

Fatal error: Call to a member function fetch_array() on a non-object in D:\xampp windows 8\htdocs\webshop\sad\index.php on line 20

  • 写回答

1条回答 默认 最新

  • donglie7268 2015-04-21 17:59
    关注

    Either your connection is not working, or your query is not correct. To check what is going wrong add this to your code

    $mysqli = new mysqli('localhost', 'root', '', 'webshop');
    //check for connection errors
    if ($mysqli->connect_error) {
        die('Connect Error (' . $mysqli->connect_errno . ') '
                . $mysqli->connect_error);
    }
    
    $sql = "SELECT  productimage FROM productlist LIMIT 5";
    $result = $mysqli->query($sql);
    //check for query errors
    if( ! $result ){
      var_dump($mysqli->error);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路