Tuesday 4 September 2018

Passing the array to the sql query does not work

This question already has an answer here:

  • mysqli_fetch_array()/mysqli_fetch_assoc()/mysqli_fetch_row() expects parameter 1 to be resource or mysqli_result, boolean given 33 answers
<?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.

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.")";

0 comments:

Post a Comment