doumi9661 2013-06-17 19:14
浏览 129
已采纳

使用PDO / PHP获取数据和循环[关闭]

I am having problems displaying the data, the page is loading fine with no error and the thead of the table is loading but its not showing any data and cannot see whats wrong. Second pair of eyes maybe needed here?

<?php

// Include config file
include('../config.php');

// Database Connection
try {
    $db = new PDO('mysql:host='.DB_HOST.';dbname='.DB_NAME.';charset=utf8', DB_USER, DB_PASSWORD);
}
catch(PDOException $e) {
    die("Could not connect to the database
");
}

// Get Raffle list
function get_raffle_list(){
    global $db;

    echo '
    <table class="table table-bordered">
        <thead>
            <tr>
                <th>ID</th>
                <th>Started</th>
                <th>Duration (days)</th>
                <th>End Date</th>
                <th>Ticket Price</th>
                <th>Percentage of Winners</th>
                <th>Tickets Purchased </th>
                <th>Total Amount</th>
                <th>Available to be Won (%)</th>
                <th>Available to be Won ($)</th>
                <th>Options</th>
                <th>Finish Raffle </th>
            </tr>
        </thead>';

        $stmt = $db->prepare("SELECT id, started, duration, ticket_price, win_percentage, available
FROM " . $db_prefix . "lotteries WHERE ended = '0' ORDER BY started DESC");
        $stmt->execute();

        echo "<tbody>";
        // loop through all result rows

        while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {

        echo '<tr>
            <td>'. $row['id'] .'</td>
            <td>'. date("m-d-Y", $row['started']) .'</td>
            <td><?php echo $duration; ?></td>
            <td>'. date("m-d-Y", $row['started'] + $row['duration']*3600*24) .'</td>
            <td>'. $row['ticket_price'] .'</td>
            <td>'. $row['win_percentage'] .'</td>
            <td>'. $row['tickets_qty'] .'</td>
            <td>'. ($row['ticket_price'] * $row['tickets_qty']) .'</td>
            <td>'. $row['available'] .'</td>
            <td>'. (floor($row['ticket_price'] * $row['tickets_qty'] * $row['available']) / 100) .'</td>
            <td><a href="flvby.php?go=dellottery&id='. $row['id'] .'">Delete</a></td>
            <td><a href="flvby.php?go=randlottery&id='. $row['id'] .'">Randomly</a></td>
            <td><a href="flvby.php?go=manlottery&id='. $row['id'] .'">Manually</a></td>
        </tr>';

        }
        echo '<tbody></table>';
}

?>
  • 写回答

2条回答 默认 最新

  • dongweihuan8610 2013-06-17 19:20
    关注

    There is a syntax error here:

    <td><?php echo $duration; ?></td>
    

    This line is already in a PHP statement, so you don't need to use <?php...?>:

    <td>' . $duration . '</td>
    

    For easier debugging in the future, consider separating PHP and HTML. A small step would be to turn off the interpreter when you output HTML, and turn it on only to execute code. Example:

    function get_raffle_list(){
        global $db;
    
        ?>
        <table class="table table-bordered">
            <thead>
                <tr>
                    <th>ID</th>
                    ...
        <?php
    
        $stmt = $db->prepare("SELECT id, started, duration, ticket_price, win_percentage, available
                FROM " . $db_prefix . "lotteries WHERE ended = '0' ORDER BY started DESC");
        $stmt->execute();
        ...
    
        ?>
        <tr>
            <td><?php echo $row['id']; ?>/td>
            <td><?php echo date("m-d-Y", $row['started']); ?></td>
            <td><?php echo $duration; ?></td>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料