我正在搜索一个表格,其中的结果可能来自3个选项中的任何一个(cata,catb或catc),然后显示结果,但是我的错误警告会一直弹出而无法显示结果?
我被困了......
<?php
include("config.php");
$cata = $_POST['cata'];
$catb = $_POST['catb'];
$catc = $_POST['catc'];
$query = "SELECT * FROM photos WHERE cata=? OR catb=? OR catc=?";
$conn = $db->prepare($query);
$conn->bind_param("sss", $cata, $catb, $catc);
if ($conn->execute()) {
$result_db = $db->query($query) or die('Error perform query!');
}
?>
<table border="1">
<tr>
<th>cata</th>
<th>catb</th>
<th>catc</th>
</tr>
<?php
while ($r = $result_db->fetch_object()) {
echo '<tr>';
echo '<td>' . $r->cata . '</td>';
echo '<td>' . $r->catb . '</td>';
echo '<td>' . $r->catc . '</td>';
echo '</tr>';
}
$db->close();
?>
答案 0 :(得分:2)
不,您创建了一个预准备语句,然后您使用了具有占位符的普通查询,这就是为什么它不起作用。执行预准备语句,然后从该预准备语句中获取结果。
$query = "SELECT * FROM photos WHERE cata=? OR catb=? OR catc=?";
$conn = $db->prepare($query);
$conn->bind_param("sss", $cata, $catb, $catc);
$conn->execute();
$conn->bind_result($cata, $catb, $catc);
?>
<table border="1">
<tr>
<th>cata</th>
<th>catb</th>
<th>catc</th>
</tr>
<?php
while ($conn->fetch()) {
echo '<tr>';
echo '<td>' . $cata . '</td>';
echo '<td>' . $catb . '</td>';
echo '<td>' . $catc . '</td>';
echo '</tr>';
}
或者如果你有mysqlnd
(mysql本机驱动程序/或者你没有那个未定义的函数),你也可以使用get_result()
:
$query = "SELECT * FROM photos WHERE cata=? OR catb=? OR catc=?";
$conn = $db->prepare($query);
$conn->bind_param("sss", $cata, $catb, $catc);
$conn->execute();
$results = $conn->get_result(); // i like this better
?>
<table border="1">
<tr>
<th>cata</th>
<th>catb</th>
<th>catc</th>
</tr>
<?php
while ($row = $results->fetch_assoc()) {
echo '<tr>';
echo '<td>' . $row['cata'] . '</td>';
echo '<td>' . $row['catb'] . '</td>';
echo '<td>' . $row['catc'] . '</td>';
echo '</tr>';
}
?>
答案 1 :(得分:0)
尝试一下。与准备好的语句配合使用非常好,并使用Bootstrap CSS来显示结果。首先连接数据库文件并包含它。接下来,布置html容器和表头。我将举一个非常大的示例,其中包含10列和一个动态按钮以链接到每个联系人(这是从我构建的数据库应用程序中获得的),以便用户在找到并编辑联系人后即可查看该联系人。
从表单开始发布到
<form class="" method="post" action="#">
<div class="form-group">
<input class="form-control mr-sm-2 form-adjust" type="text" name="query"
placeholder="Please Enter Search Term"
required></div>
<button class="btn btn-outline-success my-2 my-sm-0 form-adjust" type="submit"
name="search-submit">Start Search</button></form>
已准备好表头
<div class="table-container">
<h5 class="table-title">Below are the search results:</h5>
<table class="table table-striped">
<thead>
<tr>
<th></th>
<th>Business or Name</th>
<th>Name</th>
<th>Last Name</th>
<th>Address</th>
<th>City</th>
<th>State</th>
<th>ZIP</th>
<th>Phone</th>
<th>Email</th>
</tr>
</thead>
<tbody>
打开php脚本。服务器寻找布尔值以在POST上执行
<?php
if (isset($_POST['search-submit'])) {
$query = $_POST['query'];
$param = "%{$query}%";
$stmt = $conn->prepare("SELECT * FROM `contacts_main` WHERE `BUSINESS OR NAME`
LIKE ? OR `NAME` LIKE ? OR `LAST NAME` LIKE ? OR `ADDRESS` LIKE ? OR `ADDRESS`
LIKE ? OR `CITY` LIKE ? OR `STATE` LIKE ? OR `ZIP` LIKE ? OR `PHONE` LIKE ? OR
`EMAIL` LIKE ?");
$stmt->bind_param("ssssssssss", $param, $param, $param, $param, $param, $param, $param, $param, $param, $param);
$stmt->execute();
$stmt->store_result();
if($stmt->num_rows === 0) exit('No Search Results');
$stmt->bind_result($id, $businessorname, $name, $lastname, $address, $city, $state, $zip, $phone, $email);
$stmt->fetch();
while ($stmt->fetch()) {
$showhtml .= "<tr><td>";
$showhtml .= "<a href = \"view-contact.php?ContactID=$id\" class=\"btn btn-info\">View</a>";
$showhtml .= "</td><td>";
$showhtml .= $businessorname;
$showhtml .= "</td><td>";
$showhtml .= $name;
$showhtml .= "</td><td>";
$showhtml .= $lastname;
$showhtml .= "</td><td>";
$showhtml .= $address;
$showhtml .= "</td><td>";
$showhtml .= $city;
$showhtml .= "</td><td>";
$showhtml .= $state;
$showhtml .= "</td><td>";
$showhtml .= $zip;
$showhtml .= "</td><td>";
$showhtml .= $phone;
$showhtml .= "</td><td>";
$showhtml .= $email;
$showhtml .= "</td></tr>";
}
echo $showhtml;
}
?>
关闭php并关闭表。完成。
</tbody>
</table>
</div>