简体   繁体   中英

Mysql and PHP LIKE query generates no results on page but returns result in Mysql

Im using a multiple LiKE queries on a table in mysql, The query is being generated from another table and then sent to Mysql. The actual query when run generates results but when i run it on the page it returns nothing?? Secondly there must be a simpler way to run the same query using less LIKE command maybe

mysql_query("SELECT FROM fm_sources WHERE title,excerpt LIKE (list of items to query against)")

this is the code that im using to build the query

function parse_club_search($club)
{
    $club="'%".$club."%'";
    $club=mysql_query("SELECT * FROM fm_clubs WHERE name LIKE $club");
    while($row=mysql_fetch_assoc($club))
{
    $search_string=$row['search'];
    $search_staff=$row['staff'];
    $search_players=$row['players'];
}

$search_string=explode(",",$search_string);
$search_staff=explode(",",$search_staff);
$search_players=explode(",",$search_players);

$full_string=array_merge($search_staff,$search_players,$search_string);
array_pop($full_string);
$full_string_legnth=count($full_string);

$sql_query_holder="";

$array_track=0;

foreach($full_string as $tag)
{

   if($array_track!=($full_string_legnth-1))
   {
        echo $tag."<br/>";
        $sql_query_holder=$sql_query_holder." excerpt LIKE '%".$tag."%' OR title LIKE '%".$tag."%' OR";
        $array_track++;
    }
    else
    {
        $sql_query_holder=$sql_query_holder." excerpt LIKE '%".$tag."%' OR title LIKE '%".$tag."%'";
        $array_track++;
    }
}

echo $sql_query_holder."<br/><br/>";

$sql_query_holder="\"".$sql_query_holder."\"";
$search=mysql_query("SELECT * FROM fm_sources WHERE $sql_query_holder ORDER BY tweet_count DESC");

and this is the echoed output of the query and then when run no results.

excerpt LIKE '%Roy Hodgson%' OR title LIKE '%Roy Hodgson%' OR excerpt LIKE '%Sammy Lee%' OR title LIKE '%Sammy Lee%' OR excerpt LIKE '%Kenny Dalglish%' OR title LIKE '%Kenny Dalglish%' OR excerpt LIKE '%Brad Jones%' OR title LIKE '%Brad Jones%' OR excerpt LIKE '%Glen Johnson%' OR title LIKE '%Glen Johnson%' OR excerpt LIKE '%Paul Konchesky%' OR title LIKE '%Paul Konchesky%' OR excerpt LIKE '%Raul Meireles%' OR title LIKE '%Raul Meireles%' OR excerpt LIKE '%Daniel Agger%' OR title LIKE '%Daniel Agger%' OR excerpt LIKE '%Fabio Aurelio%' OR title LIKE '%Fabio Aurelio%' OR excerpt LIKE '%Steven Gerrard%' OR title LIKE '%Steven Gerrard%' OR excerpt LIKE '%Fernando Torres%' OR title LIKE '%Fernando Torres%' OR excerpt LIKE '%Joe Cole%' OR title LIKE '%Joe Cole%' OR excerpt LIKE '%Daniel Pacheco%' OR title LIKE '%Daniel Pacheco%' OR excerpt LIKE '%Milan Jovanovic%' OR title LIKE '%Milan Jovanovic%' OR excerpt LIKE '%Sotirios Kyrgiakos%' OR title LIKE '%Sotirios Kyrgiakos%' OR excerpt LIKE '%M axi Rodriguez%' OR title LIKE '%Maxi Rodriguez%' OR excerpt LIKE '%Dirk Kuyt%' OR title LIKE '%Dirk Kuyt%' OR excerpt LIKE '%Ryan Babel%' OR title LIKE '%Ryan Babel%' OR excerpt LIKE '%Lucas%' OR title LIKE '%Lucas%' OR excerpt LIKE '%Danny Wilson%' OR title LIKE '%Danny Wilson%' OR excerpt LIKE '%Jamie Carragher%' OR title LIKE '%Jamie Carragher%' OR excerpt LIKE '%David NGog%' OR title LIKE '%David NGog%' OR excerpt LIKE '%Pepe Reina%' OR title LIKE '%Pepe Reina%' OR excerpt LIKE '%Jay Spearing%' OR title LIKE '%Jay Spearing%' OR excerpt LIKE '%Christian Poulsen%' OR title LIKE '%Christian Poulsen%' OR excerpt LIKE '%Charles Itandje%' OR title LIKE '%Charles Itandje%' OR excerpt LIKE '%Stephen Darby%' OR title LIKE '%Stephen Darby%' OR excerpt LIKE '%Jonjo Shelvey%' OR title LIKE '%Jonjo Shelvey%' OR excerpt LIKE '%Martin Kelly%' OR title LIKE '%Martin Kelly%' OR excerpt LIKE '%Steven Irwin%' OR title LIKE '%Steven Irwin%' OR excerpt LIKE '%Martin Skrtel%' OR title LIKE '%Martin Skrt el%' OR excerpt LIKE '%Nathan Eccleston%' OR title LIKE '%Nathan Eccleston%' OR excerpt LIKE '%Daniel Ayala%' OR title LIKE '%Daniel Ayala%' OR excerpt LIKE '%Martin Hansen%' OR title LIKE '%Martin Hansen%' OR excerpt LIKE '%Peter Gulacsi%' OR title LIKE '%Peter Gulacsi%' OR excerpt LIKE '%Dean Bouzanis%' OR title LIKE '%Dean Bouzanis%' OR excerpt LIKE '%Victor Palsson%' OR title LIKE '%Victor Palsson%' OR excerpt LIKE '%Tom Ince%' OR title LIKE '%Tom Ince%' OR excerpt LIKE '%David Amoo%' OR title LIKE '%David Amoo%' OR excerpt LIKE '%Andre Wisdom%' OR title LIKE '%Andre Wisdom%' OR excerpt LIKE '%Gerardo Bruna%' OR title LIKE '%Gerardo Bruna%' OR excerpt LIKE '%Jack Robinson%' OR title LIKE '%Jack Robinson%' OR excerpt LIKE '%liverpool%' OR title LIKE '%liverpool%' OR excerpt LIKE '%lfc%' OR title LIKE '%lfc%'

The problem is this line:

$sql_query_holder="\"".$sql_query_holder."\"";

Your script doesn't output the whole query, just part of it, which it then modifies. No wonder you couldn't see what was wrong.

The query it's running is actually:

SELECT * FROM fm_sources WHERE "excerpt LIKE '%Roy Hodgson%' OR title LIKE '%Roy Hodgson%' <etc etc> " ORDER BY tweet_count DESC

You don't need those extra inverted commas

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM