Query within a $token of array: hello|today|this

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Query within a $token of array: hello|today|this

Post by simonmlewis »

Code: Select all

<?php
$q=$_GET["q"];

include "dbconn.php";
$result = mysql_query ("SELECT id, wanted FROM admin WHERE wanted LIKE '%$q%'")or die(mysql_error());
while ($row = mysql_fetch_object($result))
{
  $string = $row->wanted;
  $token = strtok($string,"|");
  while($token)
    {    
    if (preg_match("/$token/i", "$q")) { echo "<a href='#' style='text-decoration: none' title='$token'>$token</a><br/>";
    $token = strtok("|");
    }}
} mysql_free_result($result);
mysql_close($sqlconn);
?>
This is producing nothing.
I am passing through "shred" for example, and if in the wanted field, it finds "shreddy|fred|sunny", it should then find that string, and then check that each $token is similar to what I have passed thru on $q.

But it won't. Can anyone see a problem with it?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
getmizanur
Forum Commoner
Posts: 71
Joined: Sun Sep 06, 2009 12:28 pm

Re: Query within a $token of array: hello|today|this

Post by getmizanur »

it should be this

Code: Select all

$q = "shred";
$string = "shreddy|fred|sunny";
$token = strtok($string,"|");
while($token)
{
    if (preg_match("/$q/i", $token)) {
        echo "<a href='#' style='text-decoration: none' title='$token'>$token</a><br/>";
    }

    $token = strtok("|");
}
Post Reply