使用表格搜索mysqli表并显示结果 [英] Search mysqli table with form and display results
问题描述
我正在搜索一个表格,表格中的结果可能来自3个选项(cata,catb或catc)中的任何一个,然后显示结果,但是我的错误警告不断弹出,无法显示结果?
I'm searching a table with a form where the result could be from any of the 3 choices (cata, catb or catc) and then display the results, however my error warning keeps popping up and can't display the result?
我被困住了...
<?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();
?>
推荐答案
否,您创建了一条准备好的语句,然后使用了具有占位符的普通查询,这就是为什么它不起作用的原因.执行准备好的语句,然后从该准备好的语句中获取结果.
No, you created a prepared statement, then you used the normal query which has the placeholders, thats why its not working. Execute the prepared statement, then fetch the result from that prepared statement.
$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()
:
Or if you have the mysqlnd
(mysql native driver / or you will not have that undefined function), you can also use 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>';
}
?>
这篇关于使用表格搜索mysqli表并显示结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!