gpt4 book ai didi

php - 如何在php中为循环创建数据库

转载 作者:搜寻专家 更新时间:2023-10-30 23:04:11 25 4
gpt4 key购买 nike

我得到了一个 .php 文件,我想编写一个 for 循环,循环遍历数据库条目并为每个条目填充一个 div。我是一个完全的 php 菜鸟,我在网上找到的每个 for 循环似乎都不适用于我的代码。任何帮助我入门的帮助将不胜感激。

我连接数据库的php代码如下:

<?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);
?>

谢谢!

最佳答案

考虑这种面向对象 (OOP) 的方法:

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

关于php - 如何在php中为循环创建数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28803372/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com