0

CODE :

 $nerd_result = mysql_query("select * from nerd_profile where nerd_reg_no = '$reg_no'");
$nerd_data = mysql_fetch_array($nerd_result);
$tags = array();
$tags = explode(",",$nerd_data['nerd_interests']);
for($i = 0; $i < sizeof($tags)-1; $i++)
{
    if($i != sizeof($tags)-2)
    {
    $sub_query = $sub_query."`tags` like %".$tags[$i]."% or "; 
    }
    else 
    {
        $sub_query = $sub_query."`tags` like %".$tags[$i]."% ";
    }
}



 $proper_query = "select * from `qas_posts` where ".$sub_query." and `post_date` like '%$today%'";
 $result = mysql_query($proper_query);
 while($each_qas = mysql_fetch_array($result))

Description :

I am adding the like clause along with php variable in a string and concatenating it with the further variables with like clause to come. In the end when I echo I get the perfect query that I want but

mysql_fetch_array() 

does not accept that generated query rather if I hard code it , it works perfect what am I doing wrong ?? can I do that ??

3
  • 2
    This %".$tags[$i]."% needs to be this '%".$tags[$i]."%'. You're missing quotes. Also, it's typically a good idea to initialize your variables, e.g. $sub_query = "";. Commented Sep 10, 2014 at 21:36
  • ohh my goodness .... the quotes... why didn't I see that ... I have been looking on the internet for an hour now .. Thanks :) Commented Sep 10, 2014 at 21:38
  • PSA: The mysql_* functions are deprecated in PHP 5.5. It is not recommended for writing new code as it will prevent you from upgrading in the future. Instead, use either MySQLi or PDO and be a better PHP Developer. Commented Sep 10, 2014 at 21:40

1 Answer 1

2

When doing string comparisons in mysql you need to make sure you have quotes around your comparison value.

$sub_query = $sub_query."`tags` like '%".$tags[$i]."%' or ";

and

$sub_query = $sub_query."`tags` like '%".$tags[$i]."%' ";
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.