Php在HTML中获取和回显

时间:2018-03-16 08:06:39

标签: php html mysql database

<?php  
    include_once 'config.php';  

    $query = $config -> prepare("SELECT `edit`, `user_banned`, `ban_reason`, `ban_time`, `user_banner`, `ban_timestamp` FROM `samp_ban` ORDER BY `edit` ASC LIMIT 10");  
    if($query -> execute())  
    {  
        $query_results = $query->fetchAll();
         if($ban_time == 0) { $query_result["ban_time"] = "Permanent"; } 

    }  
?>

code edidetcode edidetcode edidetcode edidet

错误:未定义的变量:ban_time

3 个答案:

答案 0 :(得分:1)

你必须结合使用html和php来获取查询中的所有数据

if($row_count)  
        {  
            while($query_result = $query -> fetch()){  
                 $Username = $query_result['Username']; 
                 $Headshots = $query_result['Headshots']; 
                 $ForumName = $query_result['ForumName'] ; 
                 ?>
                 <div class="row">
                            <div class="cell" data-title="Full Name">
                                <?php echo $Username ?>
                            </div>
                            <div class="cell" data-title="Headshots">
                                <?php echo $Headshots ?>
                            </div>
                            <div class="cell" data-title="Forum Title">
                                <?php echo $ForumName ?>
                            </div>
                        </div>
                 <?php
                }
        }

答案 1 :(得分:0)

您需要更改

<?php  
    include_once 'config.php';  

    $query = $config -> prepare("SELECT `Username`, `Headshots`, `ForumName` FROM `users` ORDER BY `Headshots` DESC LIMIT 10");  

    if($query -> execute())  
    {  
        $row_count = $query -> rowCount();  

        if($row_count)  
        {  
            while($query_result = $query -> fetch())  
                 $Username = $query_result['Username']; 
                 $Headshots = $query_result['Headshots']; 
                 $ForumName = $query_result['ForumName'] ; 
        }  
    }  
?>

<?php  
    include_once 'config.php';  

    $query = $config -> prepare("SELECT `Username`, `Headshots`, `ForumName` FROM `users` ORDER BY `Headshots` DESC LIMIT 10");  

    if($query -> execute())  
    {  
        $row_count = $query -> rowCount();  

        if($row_count)  
        {  
            while($query_result = $query -> fetch()) 
            { 
                 $Username = $query_result['Username']; 
                 $Headshots = $query_result['Headshots']; 
                 $ForumName = $query_result['ForumName'] ; 
                 echo '<div class="row">';
                        echo '<div class="cell" data-title="Full Name">';
                             echo $Username;
                        echo '</div>';
                        echo '<div class="cell" data-title="Headshots">';
                            echo $Headshots;
                        echo '</div>';
                        echo '<div class="cell" data-title="Forum Title">';
                            echo $ForumName;
                        echo '</div>';
                     echo '</div>';
            }
        }  
    }  
?>

你忘记了while循环中的{}

答案 2 :(得分:0)

问题在于您尝试在两行上输出相同的变量。结果,您得到两行具有相同的结果。你必须存储从数据库到数组的行,然后创建一个for循环来输出你的html和来自该数组的数据。

PHP代码

<?php  
    include_once 'config.php';  

    $query = $config -> prepare("SELECT `Username`, `Headshots`, `ForumName` FROM `users` ORDER BY `Headshots` DESC LIMIT 10");  

    if($query -> execute())  
    {  
        $query_results = $query->fetchAll();
    }  
?>

HTML代码

<div class="limiter">

    <div class="container-table100">
        <div class="wrap-table100">
            <div class="table">

                <div class="row header">
                    <div class="cell">
                        Nickname
                    </div>
                    <div class="cell">
                        Headshots
                    </div>
                    <div class="cell">
                        Forum Name
                    </div>
                </div>

                <?php foreach( $query_results as $query_result ) { ?>
                <div class="row">
                    <div class="cell" data-title="Full Name">
                        <?php echo $query_result["Username"]; ?>
                    </div>
                    <div class="cell" data-title="Headshots">
                        <?php echo $query_result["Headshots"]; ?>
                    </div>
                    <div class="cell" data-title="Forum Title">
                        <?php echo $query_result["ForumName"]; ?>
                    </div>
                </div>
                <?php } ?>