1

I want to fetch data from database on checkbox click,here my code works for single checkbox but it shows " mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in" error on multiple checkbox, Here is my Code,

if(isset($_POST['submit'])){
if(!empty($_POST['filter'])) {
$valid=true;

$filtering=$_POST['filter'];
       $query_str=" WHERE ";
    foreach ($filtering as $value){
      if ($value=="Arsh Nagar"){
         $query_str .="stylish_name='Arsh nagar'";
      }
      else if ($value=="Padma"){
         $query_str .="stylish_name='Padma'";
      }
      else if ($value=="Kajal Yadav"){
         $query_str .="stylish_name='Kajal Yadav'";
      }
       else if ($value=="Shweta"){
         $query_str .="stylish_name='Shweta'";
      }
       else if ($value=="Harshil"){
         $query_str .="stylish_name='Harshil'";
      }
      $query_str .=" OR ";
    }
    $query_str=substr($query_str,0,-5);
    $sql="SELECT * FROM createdlook ".$query_str;

}
else{
$filter_err="Please Select atleast 1 stylish";
$valid=false;
}
}

 if($valid)
    {

    $result=mysql_query($sql); //here is the error generated
    $images = array();
    $index = 0;

    while($row = mysql_fetch_assoc($result)) // loop to give you the data in an associative array so you can use it however.
    {
         $images[$index] = $row;
         $index++;
    }

I think here is some query problem. please solve it if any error in the query

Cœur
  • 37,241
  • 25
  • 195
  • 267
Ayush Bansal
  • 11
  • 1
  • 6
  • before anything **"mysql_fetch_assoc() expects parameter 1 to be resource, boolean given"**, means that executing the query failed due to some internal error (1 %) or a syntax error in your query (99%), try dumping `$sql` and `$query_str` – Abdo Adel Jul 16 '15 at 09:11
  • your query will be `$sql="SELECT * FROM createdlookpadma ";` `$sql="SELECT * FROM createdlookShweta";` did u want this – Prashant Tapase Jul 16 '15 at 09:18
  • First try to print your query, if is it ok and secondly i think this $query_str .=" OR "; should be removed it may be a reason for error. – Yatin Khullar Jul 16 '15 at 09:19
  • Just echo `$sql` and see what it looks like. – PHPhil Jul 16 '15 at 09:20
  • I think its ur code problem. u can try this below code $query_str = " WHERE stylish_name IN ('" implode("', '", $filtering) . "')"; $sql="SELECT * FROM createdlook ".$query_str; – Ganesan Karuppasamy Jul 16 '15 at 09:22
  • When i use $query_str= " AND " then after echo $sql it prints SELECT * FROM createdlook WHERE stylish_name='Padma' OR stylish_name='Harshil' but with the "OR" it prints SELECT * FROM createdlook WHERE stylish_name='Padma' OR stylish_name='Harshil ...here Harshil has only one inverted comma what to do now – Ayush Bansal Jul 16 '15 at 09:39
  • Now it works :) ...it gives error because of substring – Ayush Bansal Jul 16 '15 at 10:04

0 Answers0