用SQL的图像轮播

时间:2015-04-21 17:05:37

标签: php sql image

我想开始一个图像轮播,图像是来自数据库的路径。

我不确定为什么这段代码不起作用。

<!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]."/>\n";
        }

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

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

错误是:

  

致命错误:在第20行的D:\ xampp windows 8 \ htdocs \ webshop \ sad \ index.php中的非对象上调用成员函数fetch_array()

1 个答案:

答案 0 :(得分:0)

您的连接无效,或查询不正确。 要检查出现了什么问题,请将其添加到您的代码中

$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);
}