Php 在此代码中如何使用LIKE运算符?
这是我的搜索引擎php代码。数据库中有三个字段:标题、描述和Url。但它将只显示与标题匹配的搜索查询。如何创建与标题、描述和Url匹配的搜索查询Php 在此代码中如何使用LIKE运算符?,php,mysql,Php,Mysql,这是我的搜索引擎php代码。数据库中有三个字段:标题、描述和Url。但它将只显示与标题匹配的搜索查询。如何创建与标题、描述和Url匹配的搜索查询 <?php ini_set('display_errors',0); // turn off php notice & errors $button = $_GET ['submit']; $search = $_GET ['search']; if(strlen($search)<=1) echo "Search ter
<?php
ini_set('display_errors',0); // turn off php notice & errors
$button = $_GET ['submit'];
$search = $_GET ['search'];
if(strlen($search)<=1)
echo "Search term too short";
else{
echo "You searched for <b>$search</b> <hr size='1'></br>";
mysql_connect("localhost","woogle","woogle");
mysql_select_db("search");
$search_exploded = explode (" ", $search);
foreach($search_exploded as $search_each)
{
$x++;
if($x==1)
$construct .="title LIKE '%$search_each%'";
else
$construct .="AND title LIKE '%$search_each%'";
}
$constructs ="SELECT * FROM searchengine WHERE $construct";
$run = mysql_query($constructs);
$foundnum = mysql_num_rows($run);
if ($foundnum==0)
echo "Sorry, there are no matching result for <b>$search</b>";
else
{
echo "$foundnum results found !<p>";
}
试试这个:
foreach($search_exploded as $search_each)
{
$x++;
$search_each_e = mysql_real_escape_string($search_each); // To help prevent SQL injection
if($x==1)
$construct .="(title LIKE '%$search_each_e%' OR description LIKE '%$search_each_e%' OR url LIKE '%$search_each_e%')";
else
$construct .="AND (title LIKE '%$search_each_e%' OR description LIKE '%$search_each_e%' OR url LIKE '%$search_each_e%')";
}
。但是,我认为使用是一种更稳健的方法。尝试以下方法:
$construct = " 1 "
foreach($search_exploded as $search_each)
{
$construct .=" AND (title LIKE '%$search_each%' OR description LIKE '%$search_each%' OR url LIKE '%$search_each%')";
}
哇,仅仅是我还是被利用的时机已经成熟了?有一点是,它看起来不像是你在描述
或url
中进行查询搜索。现在我只看到搜索标题
Woogle的代码……这很有趣。如果你不想使用参数,你至少应该使用你应该继续阅读的代码,因为你的脚本很容易受到它的攻击。