我正在使用MySQL中的数据库,我有一个表格说ABC。
我想在数据库中查询给定的搜索关键字,并在表格中显示输出。当我使用以下代码查询时,它工作正常:(游戏播放1是列名称)
if($_POST['searchby'] == "Games Type"){
$query_Recordset1 = "SELECT * FROM users_entity WHERE `Games Played 1' LIKE '%".$searchword."%'";
但是,当我尝试检查关键字是否出现在两列中时,游戏播放1或游戏播放2 时,它无效。我正在使用以下代码。
if($_POST['searchby'] == "Games Type"){
$query_Recordset1 = "SELECT * FROM elgg_users_entity WHERE `Games Played 1` OR `Games Played 2` LIKE '%".$searchword."%'";
任何人都可以建议我做错了吗?此外,当我使用上面的代码运行网页时,它会发出警告“MySQL服务器已经消失”
答案 0 :(得分:3)
if($_POST['searchby'] == "Games Type"){
$query_Recordset1 = "SELECT * FROM elgg_users_entity WHERE `Games Played 1` LIKE '%".$searchword."%' OR `Games Played 2` LIKE '%".$searchword."%'";
答案 1 :(得分:2)
您的查询应该是这样的:
$query_Recordset1 = "SELECT * FROM elgg_users_entity WHERE `Games Played 1` LIKE '%".$searchword."%' OR `Games Played 2` LIKE '%".$searchword."%'";
答案 2 :(得分:2)
<强>错误强>
<div class="form-group">
@Html.LabelFor(model => model.CountyId, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.CountyId, new List<SelectListItem>
{
new SelectListItem {Text = "Alba", Value = "1", Selected = true },
new SelectListItem {Text = "Arad", Value = "2" },
new SelectListItem {Text = "Arges", Value = "3" },
new SelectListItem {Text = "Bacau", Value = "4" },
}, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.CountyId, "", new { @class = "text-danger" })
</div>
</div>
条件。所以最终代码是
WHERE
不要在表列名称(
if($_POST['searchby'] == "Games Type"){ $query_Recordset1 = " SELECT * FROM elgg_users_entity WHERE table_coumn1 LIKE '%$searchword%' OR table_coumn2 LIKE '%$searchword%' ";
)之间使用空格。添加Games Played 1
分隔的单词。 (Games_Played_1)。强>因此,将所有小型上限作为最佳做法