Php mysql数据库按列搜索
我是mysql和php新手。我正在处理歌曲数据库。我有一个搜索脚本,它搜索数据库的所有列,并在表中显示结果。但我想让那个搜索脚本按所选列字段进行搜索。在搜索框下方,我想添加项目符号选项Php mysql数据库按列搜索,php,mysql,database,search,Php,Mysql,Database,Search,我是mysql和php新手。我正在处理歌曲数据库。我有一个搜索脚本,它搜索数据库的所有列,并在表中显示结果。但我想让那个搜索脚本按所选列字段进行搜索。在搜索框下方,我想添加项目符号选项(例如:按第1列、第2列、第3列、第4列进行搜索)您可以看到下面的图片 歌曲数据库表 +----+--------+----------+-----+-------+ | id | title | artist | key | genre | +----+--------+----------+-----+
(例如:按第1列、第2列、第3列、第4列进行搜索)
您可以看到下面的图片
歌曲数据库表
+----+--------+----------+-----+-------+
| id | title | artist | key | genre |
+----+--------+----------+-----+-------+
| 1 | xxxxx | xxx xxxxx| xxx | xxxxx |
| 2 | xxxxx | xxx xxxxx| xxx | xxxxx |
| 3 | xxxxx | xxx xxxxx| xxx | xxxxx |
| 4 | xxxxx | xxx xxxxx| xxx | xxxxx |
| 5 | xxxxx | xxx xxxxx| xxx | xxxxx |
+----+--------+----------+-----+-------+
搜索脚本:
<?php
$host = "localhost";
$user = "xxxxxxx";
$password = "xxxxxxx";
$database_name = "xxxxxx";
$pdo = new PDO("mysql:host=$host;dbname=$database_name", $user, $password, array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
));
$pdo->exec("set names utf8");
$search=$_POST['search'];
$query = $pdo->prepare("select * from table where title LIKE '%$search%' OR key LIKE '%$search%' OR genre LIKE '%$search%' OR artist LIKE '%$search%' LIMIT 0 , 100");
$query->bindValue(1, "%$search%", PDO::PARAM_STR);
$query->execute();
if (!$query->rowCount() == 0) {
$row_cnt = $result->num_rows;
printf("<p class='notice'>Your search for <span class='blue'>“</span><span class='blue'>" . $result['search'] . "</span><span class='blue'>”</span> yielded %d results.\n</p>", $row_cnt);
echo "<table class='table'>";
echo "<tr class='tablehead'>";
echo "<th>Title</th>";
echo "<th>Artist</th>";
echo "<th>Key</th>";
echo "<th>Genre</th>";
echo "</tr>";
while ($results = $query->fetch()) {
echo "<tr>";
echo "<td> <a href=publicsong.php?id=".$results['id'] . ">" "</a> " . $results['title'] . " </td>";
echo "<td>" . $results['artist'] . "</td>";
echo "<td>" . $results['key'] . "</td>";
echo "<td>" . $results['genre'] . "</td>";
echo "</tr>";
}
echo "</table>";
} else {
echo 'Nothing found';
}
?>
您应该获取单选按钮的值并进行适当的查询
$search=$_POST['search'];
$field=$_POST['radio_grp'];
switch($field)
{
case 'Title' : $filed='title' // or your column field
break;
//---similar cases
}
$query = $pdo->prepare("select * from table where $field LIKE '%$search%' LIMIT 0 , 100");
试试这个代码
将单选按钮的值设置为与列名相同
标题
艺术家
键
体裁
$search=$_POST['search'];
$search\u by=$\u POST['search\u by'];
$query=$pdo->prepare(“从表中选择*,其中$search_按“%$search%”限制为0,100”);
嘿,罗希特。我更新了我的问题,请看一看。我不知道我哪里出错了。我看到一张空白页。Thq。
$search=$_POST['search'];
$field=$_POST['radio_grp'];
switch($field)
{
case 'Title' : $filed='title' // or your column field
break;
//---similar cases
}
$query = $pdo->prepare("select * from table where $field LIKE '%$search%' LIMIT 0 , 100");
<input type="radio" name="search_by" value="title">title<br>
<input type="radio" name="search_by" value="artist">artist<br>
<input type="radio" name="search_by" value="key">key<br>
<input type="radio" name="search_by" value="genre">genre<br>
$search=$_POST['search'];
$search_by=$_POST['search_by'];
$query = $pdo->prepare("select * from table where $search_by LIKE '%$search%' LIMIT 0 , 100");