1

I have been given a .php file and I would like to code a for loop that loops through database entries and populates a div for each entry. I am a total php noob, and every for loop I've found online doesn't seem to work with the code I have. Any help to get me started would be greatly appreciated.

My php code to connect to the database is as follows:

<?php
    require 'mysqliconnection.php';
    require 'top_bottom_functions.php';

    $id = $_GET['id'];
    $stmt_get_details = mysqli_prepare($connect, "SELECT name, nickname, birth_day, birth_place, age, division, fighter_record, stance, height, reach, directory, biography, flag, record FROM fighters_details WHERE id=?");
    if ($stmt_get_details) {
        mysqli_stmt_bind_param($stmt_get_details, "s", $id);
        mysqli_stmt_execute($stmt_get_details);
        mysqli_stmt_bind_result($stmt_get_details, $name, $nickname, $birth_day, $birth_place, $age, $division, $fighter_record, $distance, $height, $reach, $directory, $biography, $flag, $record);
}

    mysqli_stmt_fetch($stmt_get_details);

    $height = html_entity_decode($height);

    $reach = html_entity_decode($reach);

    mysqli_stmt_close($stmt_get_details);
?>

Thank you!

2 Answers 2

2

Consider this object oriented (OOP) approach:

$mysqli = mysqli_connect('localhost', 'username', 'password', 'db_name');

$stmt = $mysqli->prepare("SELECT name, nickname, birth_day, birth_place, age, division, fighter_record, stance, height, reach, directory, biography, flag, record FROM fighters_details WHERE id=?");
if ($stmt) {
    $stmt->bind_param("s", $_GET['id']);
    $stmt->execute();
    $stmt->bind_result($name, $nickname, $birth_day, $birth_place, $age, $division, $fighter_record, $stance, $height, $reach, $directory, $biography, $flag, $record);

    while ($stmt->fetch()) {
        echo '<div>Name'.$name.', Nickname'.$nickname.', etc...</div>';
    }

    $stmt->close();
}

$mysqli->close();
Sign up to request clarification or add additional context in comments.

Comments

0

Hope this helps.

while(mysqli_stmt_fetch($stmt_get_details))
{
  echo "<div> name:"$name."</div>";
  .
  .
  //so on for other columns
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.