如何在php中为循环创建数据库
我得到了一个如何在php中为循环创建数据库,php,mysql,database,loops,Php,Mysql,Database,Loops,我得到了一个.php文件,我想编写一个for循环,循环遍历数据库条目,并为每个条目填充一个div。我是一个十足的PHPNoob,我在网上找到的每个for循环似乎都不能与我的代码一起工作。如果您能帮助我开始学习,我们将不胜感激 我连接数据库的php代码如下: <?php require 'mysqliconnection.php'; require 'top_bottom_functions.php'; $id = $_GET['id']; $stmt_ge
.php
文件,我想编写一个for循环,循环遍历数据库条目,并为每个条目填充一个div。我是一个十足的PHPNoob,我在网上找到的每个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);
?>
谢谢大家! 希望这有帮助
while(mysqli_stmt_fetch($stmt_get_details))
{
echo "<div> name:"$name."</div>";
.
.
//so on for other columns
}
while(mysqli_stmt_fetch($stmt_get_details))
{
echo“name:$name.”;
.
.
//其他专栏也是如此
}
考虑这种面向对象(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();
$mysqli=mysqli_connect('localhost','username','password','db_name');
$stmt=$mysqli->prepare(“选择姓名、昵称、出生日期、出生地点、年龄、分区、拳击手记录、站姿、身高、距离、目录、传记、旗帜、拳击手记录详情,其中id=?”;
如果($stmt){
$stmt->bind_param(“s”,$_GET['id']);
$stmt->execute();
$stmt->bind_result($name、$昵称、$birth_day、$birth_place、$age、$division、$fighter_record、$stance、$height、$reach、$directory、$传记、$flag、$record);
而($stmt->fetch()){
回显“名称”。$名称。“$昵称”等;
}
$stmt->close();
}
$mysqli->close();