简体   繁体   English

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

[英]Creating a list using data from MySQL database

I'm trying to create a basic product list that takes data from the MySQL database and display on screen of the website. 我正在尝试创建一个基本产品列表,该列表从MySQL数据库获取数据并显示在网站屏幕上。

I currently have the following code but still needs some work, it currently only displays the name and category with an error message saying:Warning: printf(): Too few arguments in C:\\xampp\\htdocs\\ICTDBS504\\list items.php on line 41 () () 我目前有以下代码,但仍需要一些工作,它目前仅显示名称和类别,并显示一条错误消息:警告:printf():C:\\ xampp \\ htdocs \\ ICTDBS504 \\ list items.php上的参数太少第41行()()

I also need to add a photo in but I am unsure on how and where that goes in the code. 我还需要在其中添加照片,但是不确定代码中的方式和位置。 Should the photo be apart of the html or saved on the database? 照片应该放在html之外还是保存在数据库中? and if on the database how does that get written into the php code 如果在数据库上如何将其写入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();
?>

In this part of your code you have type more arguments in printf() : 在代码的这一部分,您在printf()输入了更多参数:

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

Printf have 3 arguments but you only have put 2: $row["Name"] and $row["Category"] . Printf有3个参数,但您只输入了2个: $row["Name"]$row["Category"]

To solve the error you have to fix the arguments or add 1 more datas. 要解决该错误,您必须修复参数或添加1个以上数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM