使用MySQL数据库中的数据创建列表

时间:2017-07-06 06:46:25

标签: php mysql database list display

我正在尝试创建一个基本产品列表,该列表从MySQL数据库中获取数据并显示在网站的屏幕上。

我目前有以下代码但仍需要一些工作,它目前只显示名称和类别,并显示错误消息:警告:printf():C:\ xampp \ htdocs \ ICTDBS504 \ list items中的参数太少第41行的.php ()()

我还需要添加一张照片,但我不确定代码的方式和位置。照片应该是html的一部分还是保存在数据库中?如果在数据库中如何将其写入php代码

<?php
    $mysqli = new mysqli("localhost", "root", "", "etrading");

    /* check connection */
    if ($mysqli->connect_errno) {
        printf("Connect failed: %s\n", $mysqli->connect_error);
        exit();
    }

    $query = "SELECT Name, Category, Price, Duration, Photo FROM item ORDER by ItemID LIMIT 3";
    $result = $mysqli->query($query);

    /* numeric array */
    $row = $result->fetch_array(MYSQLI_NUM);
    printf ("%s (%s)\n", $row[0], $row[1]);

    /* associative array */
    $row = $result->fetch_array(MYSQLI_ASSOC);
    printf ("%s (%s) (%s)\n", $row["Name"], $row["Category"]);

    /* associative and numeric array */
    $row = $result->fetch_array(MYSQLI_BOTH);
    printf ("%s (%s) (%s)\n", $row[0], $row["Price"], $row["Duration"]);

    /* free result set */
    $result->free();

    /* close connection */
    $mysqli->close();
?>

1 个答案:

答案 0 :(得分:0)

在代码的这一部分中,您在printf()中输入了更多参数:

    /* associative array */
    $row = $result->fetch_array(MYSQLI_ASSOC);
    printf ("%s (%s) (%s)\n", $row["Name"], $row["Category"]);

Printf有3个参数,但您只放了2个:$row["Name"]$row["Category"]

要解决错误,您必须修复参数或添加1个数据。