0

我在显示数据时遇到问题,页面加载正常,没有错误,并且表格的标题正在加载,但它没有显示任何数据并且看不到有什么问题。这里可能需要第二双眼睛?

<?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\n");
}

// 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>';
}

?>
4

2 回答 2

1

这里有一个语法错误:

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

此行已在 PHP 语句中,因此您无需使用<?php...?>

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

为了将来更容易调试,请考虑将 PHP 和 HTML 分开。一个小步骤是在您输出 HTML 时关闭解释器,并仅在执行代码时打开它。例子:

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>
于 2013-06-17T19:20:44.953 回答
0

$stmt->fetch()在错误时返回 false ,这将打破您的循环。

请检查 PDO 错误以找出循环未执行的原因。您可以在此处找到有关 PDO 错误的信息:PDO::errorInfo

你需要类似的东西:

print_r($dbh->errorInfo());
于 2013-06-17T19:26:41.227 回答