-7

I cannot work out why I am receiving this error:

mysql_fetch_assoc() expects parameter 1 to be resource, boolean given on line 32

I have tried searching for the answer but have had no such luck.

I am self taught learning to code and any help would be greatly appreciated.

<table>
    <thead>
        <tr>
            <td>First Name</td>
            <td>Last Name</td>
            <td>Email</td>
        </tr>
    </thead>
    <tbody>
    <?php

    $mysql_host = 'localhost';
    $mysql_user = 'root';
    $mysql_pass = '';
    mysql_connect($mysql_host, $mysql_user, $mysql_pass);


    /* #1 get properties and it's data  */
    $seller_id="SELECT id FROM seller WHERE coupon_id='ab2345'" or die(mysql_error());
    $property="SELECT bedrooms, bathrooms, parking FROM property WHERE seller_id='{$seller_id}'" or die(mysql_error());
    $results = mysql_query($property);

    /* #2 store property data in variable */
    $property_row = mysql_fetch_assoc($results);
    $bathrooms = $property_row['bathrooms'];
    $bedrooms = $property_row['bedrooms'];
    $parking = $property_row['parking'];

    /*#3 get buyers (& their data) using property data as inputs */
    $buyers="SELECT * FROM buyers WHERE bedrooms={$bedrooms} AND bathrooms={$bathrooms} AND parking={$parking}" or die(mysql_error());
    $results = mysql_query($buyers);

    /* #4 loop through buyers */
    if (mysql_num_rows($results) < 1) echo "Oh no it seems like their are no buyers for your property"; else {
    while ($row = mysql_fetch_assoc($results)) {
    ?>
            <tr>
                <td><?php echo $row['f_name']?></td>
                <td><?php echo $row['l_name']?></td>
                <td><?php echo $row['email']?></td>
            </tr>

        <?php
        }
    }
        ?>
        </tbody>
        </table>
Brian Tompsett - 汤莱恩
  • 5,438
  • 68
  • 55
  • 126
  • 2
    This question, or something closely related to it, comes up several time a day on [so]. Search a little harder. –  Feb 23 '14 at 04:09
  • You're using quotes in `WHERE seller_id='{$seller_id}'` yet not in `WHERE bedrooms={$bedrooms} AND bathrooms={$bathrooms} AND parking={$parking}` --- why not just do `WHERE bedrooms='$bedrooms' AND bathrooms='$bathrooms' AND parking='$parking'` – Funk Forty Niner Feb 23 '14 at 04:09
  • 1
    `$str = "foo" or die(mysql_error())` is **UTTERLY** pointless. Exactly how do you expect this string assignment to fail? You need to do the `or die()` on your mysql calls. Anywhere else is useless. – Marc B Feb 23 '14 at 04:19
  • You're welcome. Did it fix it? @user3342398 – Funk Forty Niner Feb 23 '14 at 04:27
  • Sorry for the late reply Fred - Yes it was that and another small syntax error :) know im just having trouble with the while loop – user3342398 Feb 23 '14 at 06:35
  • I now have the code completely working, thank you everyone! – user3342398 Feb 23 '14 at 06:37

1 Answers1

1

mysql returns a resource when there is no error in your query but returns false (a booean data type and an unsuitable parameter for mysql_fetch...) when there is an error.

Your PHP code should check the return code to make sure that it is "truthy" (ie not false) before trying to use it to fetch. If the return is false, there are two mysql functions that you can use to determine the problem. One ,mysql_error(), returns an error number (good for switch statements to process) the other, mysql_errmsg() returns a textual error message (good for programmers to read).

One of the commenters told you what the error in your query was but that does not deal with the structural flaw in your code where you do not check for a good return value before trying to use it.

Ted Cohen
  • 968
  • 9
  • 16