コードはデータベース内の結果の数を正しく取得していますが、結果が表示されていません。結果を表示する代わりに、「else」条件メッセージ「結果を表示できませんでした」を表示しています。
ここで何が間違っているのかわかりません...たくさんの記事を見てきましたが、答えを見つけることができませんでした。
<font size="+3" face="Verdana">Database</font>
<br><br>
<form name="form" action="" method="get">
<input type="text" name="q" size="60" />
<input type="submit" name="Submit" value="Search">
</form>
<?php
include 'connect.php';
$var = $_GET['q'];
echo $var;
try {
// Find out how many items are in the table
$total = $conn->query('
SELECT
COUNT(*)
FROM
customers
')->fetchColumn();
// How many items to list per page
$limit = 20;
// How many pages will there be
$pages = ceil($total / $limit);
// What page are we currently on?
$page = min($pages, filter_input(INPUT_GET, 'page', FILTER_VALIDATE_INT, array(
'options' => array(
'default' => 1,
'min_range' => 1,
),
)));
// Calculate the offset for the query
$offset = ($page - 1) * $limit;
// Some information to display to the user
$start = $offset + 1;
$end = min(($offset + $limit), $total);
// The "back" link
$prevlink = ($page > 1) ? '<a href="?page=1" title="First page">«</a> <a
href="?page=' . ($page - 1) . '" title="Previous page">‹</a>' : '<span
class="disabled">«</span> <span class="disabled">‹</span>';
// The "forward" link
$nextlink = ($page < $pages) ? '<a href="?page=' . ($page + 1) . '" title="Next
page">›</a> <a href="?page=' . $pages . '" title="Last page">»</a>' :
'<span class="disabled">›</span> <span class="disabled">»</span>';
// Display the paging information
echo '<div id="paging"><p>', $prevlink, ' Page ', $page, ' of ', $pages, ' pages,
displaying ', $start, '-', $end, ' of ', $total, ' results ', $nextlink, ' </p></div>';
// Prepare the paged query
$stmt = $conn->prepare('
SELECT * FROM customers
WHERE
statusstring LIKE :statusstring OR
company LIKE :company OR
city LIKE :city OR
state LIKE :state OR
zip LIKE :zip OR
mainphone LIKE :mainphone OR
contact LIKE :contact OR
function LIKE :function OR
cellphone LIKE :cellphone OR
email LIKE :email OR
nextcontactdate LIKE :nextcontactdate
LIMIT :limit
OFFSET :offset');
// Bind the query params
$stmt->execute(array(':statusstring' => '%'.$_GET['q'].'%',
':company' => '%'.$_GET['q'].'%',
':city' => '%'.$_GET['q'].'%',
':state' => '%'.$_GET['q'].'%',
':zip' => '%'.$_GET['q'].'%',
':mainphone' => '%'.$_GET['q'].'%',
':contact' => '%'.$_GET['q'].'%',
':function' => '%'.$_GET['q'].'%',
':cellphone' => '%'.$_GET['q'].'%',
':email' => '%'.$_GET['q'].'%',
':nextcontactdate' => '%'.$_GET['q'].'%',
':limit' => $limit,
':offset' => $offset));
// Do we have any results?
if ($stmt->rowCount() > 0) {
// Define how we want to fetch the results
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$iterator = new IteratorIterator($stmt);
// Display the results
foreach ($iterator as $row) {
echo "<tr>
<td align=center>"; if($row['status']==1)
{
echo "Contacted";
}
elseif($row['status']==2)
{
echo "Uncontacted";
}
elseif($row['status']==3)
{
echo "Do Not Call";
}
echo "</td>
<td align=center>". $row['company'] ."</td>
<td align=center>". $row['city'] ."</td>
<td align=center>". $row['state'] ."</td>
<td align=center>". $row['zip'] . "</td>
<td align=center>". $row['mainphone'] ."</td>
<td align=center>". $row['contact'] ."</td>
<td align=center>". $row['function'] ."</td>
<td align=center>". $row['cellphone'] ."</td>
<td align=center>". $row['email'] ."</td>
<td align=center>". $row['lastcontactdate'] ."</td>
<td align=center>". $row['nextcontactdate'] ."</td>
<td align=center valign=middle width=100>
<form action=customerdetails.php method=POST>
<input type=hidden value=" . $row['id'] . " name=id>
<input type=image src=images/details.png height=30>
</form>
</td></tr>";
}
} else {
echo '<p>No results could be displayed.</p>';
}
} catch (Exception $e) {
echo '<p>', $e->getMessage(), '</p>';
}
?>