简体   繁体   中英

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.

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

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? and if on the database how does that get written into the php code

<?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() :

    /* 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"] .

To solve the error you have to fix the arguments or add 1 more datas.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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