【问题标题】:How to create a database for loop in php如何在php中为循环创建数据库
【发布时间】:2015-03-02 05:44:16
【问题描述】:

我得到了一个.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);
?>

谢谢!

【问题讨论】:

    标签: php mysql database loops


    【解决方案1】:

    考虑这种面向对象 (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();
    

    【讨论】:

      【解决方案2】:

      希望这会有所帮助。

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

      【讨论】:

        猜你喜欢
        • 2014-06-19
        • 2021-11-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-01-14
        • 2019-01-17
        • 1970-01-01
        • 2022-12-04
        相关资源
        最近更新 更多