0

I have created a database having the details of mobiles but when i go for adding the filter using checkbox it give me error

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in /home/mobodr/public_html/search.php on line 16

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in /home/mobodr/public_html/search.php on line 23

For which my HTML is

<form action='search.php' method='get'>
<section id="portal">
<input class="tick" type="checkbox" name="comp" value="LG">LG</input><br/>
<input class="tick" type="checkbox" name="comp" value="Motorola">Motorola</input><br/>
<input class="tick" type="checkbox" name="comp" value="Nokia">Nokia</input><br/>
<input class="tick" type="checkbox" name="comp" value="Microsoft">Microsoft</input><br/>
<input class="tick" type="checkbox" name="comp" value="Xolo">Xolo</input><br/>
<input class="tick" type="checkbox" name="comp" value="Micromax">Micromax</input><br/>
<input class="tick" type="checkbox" name="comp" value="Karbonn">Karbonn</input><br/>
<input class="tick" type="checkbox" name="comp" value="iBall">iBall</input><br/>
<input class="tick" type="checkbox" name="comp" value="Spice">Spice</input><br/>
<input class="tick" type="checkbox" name="comp" value="Videocon">Videocon</input><br/>
<input class="tick" type="checkbox" name="comp" value="Fly">Fly</input><br/>
<input class="tick" type="checkbox" name="comp" value="Zen">Zen</input><br/>
<input class="tick" type="checkbox" name="comp" value="LAVA">LAVA</input><br/>
<input class="tick" type="checkbox" name="comp" value="Sony">Sony</input><br/>
<input class="tick" type="checkbox" name="comp" value="Acer">Acer</input><br/>
<input class="tick" type="checkbox" name="comp" value="Lenovo">Lenovo</input><br/>
<input class="tick" type="checkbox" name="comp" value="Apple">Apple</input><br/>
<input class="tick" type="checkbox" name="comp" value="Idea">Idea</input><br/>
<input class="tick" type="checkbox" name="comp" value="Pansonic">Panasonic</input><br/>
<input class="tick" type="checkbox" name="comp" value="Oppo">Oppo</input><br/>
<input class="tick" type="checkbox" name="comp" value="samsung">Samsung</input><br/>

    </section>
    <input value="Search" type="submit" style="border-radius: 8px; background: #fff;"></input>
    </form>

I want when i click on the particular check box then it show me the mobiles of only that brand. But when i try to do so it gives me error. For this my PHP script is

<?php
$com = $_REQUEST['comp'];
  mysql_connect("localhost","","");
    //seclect db
    mysql_select_db("mobodr_mobile");
    //connection string
    $dat = mysql_query("SELECT * FROM Devices2 ORDER BY comp WHERE comp=". $com ." ASC" );
    $img = 'img';
$name = 'name';
$dis = 'dis';
$comp = 'comp';
$ebay = 'ebay';
$amazon = 'amazon';
$res = mysql_fetch_assoc($dat);
while ($res = mysql_fetch_assoc($dat)){echo "<figure><div style='text-align:center;'><img class='prev' src='" . $res[$img] . "'/></div><figcaption><a href='http://mobodroid.net/show.php?q=".  $res[$name] . "'><h3>" . $res[$name] . "</h3></a><img width='80px' height='40px' src='". $comimg ."'/><a class='mor' href='http://mobodroid.net/show.php?q=".  $res[$name] . "'>View Details</a></figcaption></figure>";

}?>
8
  • 1
    ORDER BY must come after WHERE. Commented May 12, 2015 at 13:01
  • Where are you sending '$com' (supposed to be in url)? Commented May 12, 2015 at 13:01
  • 1
    Please, stop using mysql_* functions. They are no longer maintained and are officially deprecated. Learn about prepared statements instead, and consider using PDO, it's not as hard as you think. Commented May 12, 2015 at 13:03
  • It could be useful to know which errors it displays. When you're developing, a good way to be sure not to miss any error is to set the error reporting mode : php.net/manual/fr/function.error-reporting.php Commented May 12, 2015 at 13:04
  • Now that it works, tell us what happens when you choose more than one checkbox. Commented May 12, 2015 at 13:19

2 Answers 2

2

Strings has to be in quotes, add them around $com.

$dat = mysql_query("SELECT * FROM Devices2 WHERE comp='". $com ."' ASC);
                                                      ^          ^
Sign up to request clarification or add additional context in comments.

Comments

1

You did two mistakes:-

1.String will be in double quotes as said by @panther:-

$dat = mysql_query("SELECT * FROM Devices2 ORDER BY comp WHERE comp='". $com ."' ASC" );

2.ORDER BY must came after WHERE clause.

So the final query will be:-

$dat = mysql_query("SELECT * FROM Devices2 WHERE comp='". $com ."' ORDER BY comp ASC" );

Note:- Also please stop using mysql_* because they are officially deprecated. Use mysqli_* or PDO.

1 Comment

thanks and please vote up @panther also. because he also gives you valuable in formation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.