我有一个输入类型文本并作为搜索栏提交到我的页面上。我希望我的 php 脚本打印表中包含插入字符串作为其自身一部分的所有名称。目前,此脚本仅在文本输入中写入全名时才打印信息。但我也希望它能够显示其中是否只写了一部分。
private function search(){
if (isset($_POST['submit'])) {
# Database connection
$db = $this->db;
$db->set_charset('utf8');
# check connection
if (!$db) {
echo "connection fail";
}
# HTML output (pt. 1)
$output = '
<table>
';
# SQL query
$q = '
SELECT imeStud, prezStud
FROM stud
WHERE imeStud LIKE ?
';
# process SQL query
$search_input = $_POST['search'];
if ($stmt = $db->prepare($q)) {
$stmt->bind_param('s',$search_input);
$stmt->bind_result($ime,$prezime);
$stmt->execute();
while ($stmt->fetch()) {
$output .= '
<tr><td>'.$ime.'</td><td>'.$prezime.'</td></tr>
';
}
}
# HTML output (pt. 3)
$output .= '</table>';
echo $output;
}
}#endfunc(search())
最佳答案
你的东西最多,只需在搜索词前后添加通配符即可
- $search_input = $_POST['search'];
+ $search_input = '%' . $_POST['search'] . '%';
或
- $stmt->bind_param('s',$search_input);
+ $stmt->bind_result('s','%' . $search_input . '%');
关于php - 仅搜索部分数据库字符串时打印数据库字符串(通过 PHP 语句),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33858779/