Reputation: 245
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!
Upvotes: 1
Views: 95
Reputation: 1382
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();
Upvotes: 2
Reputation: 814
Hope this helps.
while(mysqli_stmt_fetch($stmt_get_details))
{
echo "<div> name:"$name."</div>";
.
.
//so on for other columns
}
Upvotes: 0