php - Passing array to sql query not working -
this question has answer here:
<?php require_once("dbdata.php"); if(mysql_connect($server_name,$db_user,$db_pass)) { if(mysql_select_db($database_name)) { $display_scores = "select distinct user_id, quiz_id, parent_category,category_name, score custom_question_details user_id = 3"; $results = mysql_query($display_scores); $scores = array(); $s_p = array(); while($rows = mysql_fetch_assoc($results)) { $scores[] = $rows; $s_p [] = $rows['parent_category']; } $imp = implode(',',$s_p); $child_sql = "select distinct category_name custom_question_details parent_category in (".$imp.")"; $child_result = mysql_query($child_sql); $child_array = array(); while($fetch_child_results = mysql_fetch_assoc($child_result)) { $child_array[] = $fetch_child_results; } print_r($child_array); } } ?>
i getting error
warning: mysql_fetch_assoc() expects parameter 1 resource, boolean given in line ...
while passing $imp
$child_sql
query. if give string 'sample' statically in db instead of $imp
variable obtain result. problem in passing $imp
variable in passing query.
you must enquote values in in
clause
try this
$s_p [] = "'".$rows['parent_category']."'";
instead of
$s_p [] = $rows['parent_category'];
now use
$imp = implode(',',$s_p); $child_sql = "select distinct category_name custom_question_details parent_category in (".$imp.")";
Comments
Post a Comment