gpt4 book ai didi

php - 使用 PDO/PHP 获取数据并循环

转载 作者:行者123 更新时间:2023-11-29 13:47:49 25 4
gpt4 key购买 nike

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

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

?>

最佳答案

这里有一个语法错误:

<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>

关于php - 使用 PDO/PHP 获取数据并循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17155045/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com