将数组传递给sql查询无法正常工作[重复]

<?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 FROM custom_question_details where 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 FROM custom_question_details WHERE 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 am getting this error

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in line ...

while passing $imp to $child_sql query. But if I give some string like 'Sample' statically which is in db instead of $imp variable i obtain the result. what is the problem in passing $imp variable in passing the query.

</div>

you must enquote values in IN clause

try this

$s_p [] = "'".$rows['Parent_Category']."'";

instead of

$s_p [] = $rows['Parent_Category'];

now use it

$imp = implode(',',$s_p);
     $child_sql = "SELECT DISTINCT Category_Name FROM custom_question_details WHERE Parent_Category IN (".$imp.")";

Change your sql query to this:

$child_sql = "SELECT DISTINCT Category_Name FROM custom_question_details WHERE Parent_Category IN ('".$imp."')";

Try this ...

replace the code..

$child_sql = "SELECT DISTINCT Category_Name FROM custom_question_details WHERE   Parent_Category IN ( $imp )";

Validation is important.

After $child_result = mysql_query($child_sql);

type if(!$child_result) die(mysql_error());

Note: die(); is not a better option. just saying so that you know what's wrong with your query.

$imp ='';
while($rows = mysql_fetch_assoc($results))
     {
        $scores[] = $rows; 
        $imp = "'".$rows['Parent_Category']."', ";
     }
    // $imp = implode(',',$s_p);

$imp=rtrim($imp, ", ");
$child_sql = "SELECT DISTINCT Category_Name FROM custom_question_details WHERE Parent_Category IN (".$imp.")";
----
-----