mysql - Generating a sql query in php using for loop -
code :
$nerd_result = mysql_query("select * nerd_profile 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` %".$tags[$i]."% or "; } else { $sub_query = $sub_query."`tags` %".$tags[$i]."% "; } } $proper_query = "select * `qas_posts` ".$sub_query." , `post_date` '%$today%'"; $result = mysql_query($proper_query); while($each_qas = mysql_fetch_array($result))
description :
i adding clause along php variable in string , concatenating further variables clause come. in end when echo perfect query want
mysql_fetch_array()
does not accept generated query rather if hard code , works perfect doing wrong ?? can ??
when doing string comparisons in mysql need make sure have quotes around comparison value.
$sub_query = $sub_query."`tags` '%".$tags[$i]."%' or ";
and
$sub_query = $sub_query."`tags` '%".$tags[$i]."%' ";
Comments
Post a Comment