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>';
}
?>
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>
The $stmt->fetch()
will return false on error which will break out of your loop.
Please check the PDO errors to find why the loop is not executing. You can find information on PDO errors here: PDO::errorInfo
You'll need something like:
print_r($dbh->errorInfo());