-2

I get:

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

as an error but I don't know why!

I have to do this exercise for school and so far so good but now I get this error and I have been stuck for a while.

I am quiet new to this whole thing so I hope my code isn't to bad. My code is:

mysql_connect ("localhost","root","usbw");
mysql_select_db("classicmodels");

function productLinesDropDown(){
    $result = mysql_query("SELECT productLine , textDescription FROM `productlines`");
    echo "<form method='post' action=dropdownlist.php><select name='productlijn'>";
    while($data = mysql_fetch_assoc($result)){
        echo "<option value'" . $data["productLine"] . "'>" . $data["productLine"] . "</option>";
    }
    echo "</select><input name=Submit type=submit value=Submit></form>";
}

if(isset($_POST['Submit'])){
    $productlijn = $_POST['productlijn'];
    toonProductsProductline($productlijn);
}
function toonProductsProductLine($productline) {
    $gegevens = mysql_query("SELECT productName , productScale , productDescritpion , productLine FROM `product`");
    echo "<table><tr><td>productName</td><td>productScale</td><td>productDescription</td></tr>";
    while($data = mysql_fetch_assoc($gegevens)){
        if($data["productLine"] == $productline){
            echo "<tr>";
            echo "<td>" . $data["productName"] . "</td>";
            echo "<td>" . $data["productScale"] . "</td>";
            echo "<td>" . $data["productDescription"] . "</td>";
            echo "</tr>";
        }
    }
    echo "</table>";
}
productLinesDropDown();
Peyman Mohamadpour
  • 17,954
  • 24
  • 89
  • 100
  • Welcome to SO. Please consider that your title is useless, and that the only person that should be doing your homework is you. If your school is teaching you to use mysql_ functions, walk in there and smack your teacher in the face. Read the manual, and check your query for errors. `if(!$result){...} – mickmackusa Feb 26 '17 at 13:31
  • Possible duplicate of [mysqli\_fetch\_array()/mysqli\_fetch\_assoc()/mysqli\_fetch\_row() expects parameter 1 to be resource or mysqli\_result, boolean given](http://stackoverflow.com/questions/2973202/mysqli-fetch-array-mysqli-fetch-assoc-mysqli-fetch-row-expects-parameter-1) – Qirel Feb 26 '17 at 13:31
  • 2
    Your query is failing - so you'll need to figure out why. PHP is willing to tell you, if you let it. Use `echo mysql_error();` after the failing query. Also, as a sidenote: Usage of `mysql_*` functions are **deprecated** and outdated, you should be learning PDO or `mysqli_*` (note the `i`) instead, where you should take advantage of prepared statements with placeholders. – Qirel Feb 26 '17 at 13:33
  • Duplicate of: http://stackoverflow.com/questions/2973202/mysqli-fetch-array-mysqli-fetch-assoc-mysqli-fetch-row-expects-parameter-1 – mickmackusa Feb 26 '17 at 14:21
  • Yes we are currently learning to using msqli but we first learned it the old way. But I have to re-do the test so I should learn the old way first. – Niels Schuitman Feb 26 '17 at 14:23

2 Answers2

0

Reading the documentation we are told

For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.

Your code should test for this boolean BEFORE your while loop and perform some appropriate action. In your case I would suggest getting the value of mysql_error() in order to determine what the problem is with your SQL.

Ben Wainwright
  • 4,224
  • 1
  • 18
  • 36
0

As you can read in manual

Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE.

This text is about mysqli but in this case it works as same as mysql.

You have to check if your query failed or returned empty result (contains FALSE value) or not.

Instead

while($data = mysql_fetch_assoc($gegevens)){

do something like that

while($gegevens !== FALSE && $data = mysql_fetch_assoc($gegevens)){

Also you can add else to e.g. display information about empty result.

Wolen
  • 874
  • 6
  • 15