Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jan 2003
    Location
    U.S.A
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    need help with the search engine.

    Hi everyone,

    I'm trying to create the search engine for my project, but I'm getting into some problem with this script, hoping that everyone in here can help me. When I run this script I got the following error display. Thank a lot.

    "Warning: mysql_fetch_assoc(): 2 is not a valid MySQL result resource in c:\program files\apache group\apache\htdocs\hotel_sample\search.php on line 100"




    <?php require_once('../Connections/hotelsample.php'); ?>
    <?php

    if($HTTP_POST_VARS["bed_option"] == "less")
    {
    $bComp = "<";
    }
    else if($HTTP_POST_VARS["bed_option"] == "more")
    {
    $bComp =">";
    }
    else
    {
    $bComp ="=";
    }
    ?>
    <?php
    if($HTTP_POST_VARS["price_option"] == "above")
    {
    $pComp = ">";
    }
    else
    {
    $pComp = "<=";
    }
    ?>
    <?php
    $MM_pComp_rsSearch = "<=";
    if (isset($pComp)) {
    $MM_pComp_rsSearch = (get_magic_quotes_gpc()) ? $pComp : addslashes($pComp);
    }
    $MM_pNum_rsSearch = "0";
    if (isset($HTTP_POST_VARS["price"])) {
    $MM_pNum_rsSearch = (get_magic_quotes_gpc()) ? $HTTP_POST_VARS["price"] : addslashes($HTTP_POST_VARS["price"]);
    }
    $MM_bedComp_rsSearch = "<";
    if (isset($bComp)) {
    $MM_bedComp_rsSearch = (get_magic_quotes_gpc()) ? $bComp : addslashes($bComp);
    }
    $MM_bedNum_rsSearch = "0";
    if (isset($HTTP_POST_VARS["bed"])) {
    $MM_bedNum_rsSearch = (get_magic_quotes_gpc()) ? $HTTP_POST_VARS["bed"] : addslashes($HTTP_POST_VARS["bed"]);
    }
    mysql_select_db($database_hotelsample, $hotelsample);
    $query_rsSearch = sprintf("SELECT ID, price, bed, description, number FROM room WHERE bed %s %s AND price %s %s", $MM_bedComp_rsSearch,$MM_bedNum_rsSearch,$MM_pComp_rsSearch,$MM_pNum_rsSearch);
    $rsSearch = mysql_query($query_rsSearch, $hotelsample) or die(mysql_error());
    $row_rsSearch = mysql_fetch_assoc($rsSearch);
    $totalRows_rsSearch = mysql_num_rows($rsSearch);
    ?>
    <form name="form1" method="post" action="search.php">
    <table width="400" border="2" align="center" bordercolor="#0000FF">
    <tr align="center">
    <td colspan="3">Search for a room</td>
    </tr>
    <tr>
    <td width="130" align="right">Number of beds :</td>
    <td width="87"><select name="bed_option" id="bed_option">
    <option value="more" selected>More than</option>
    <option value="less">Less than</option>
    <option value="equal">Equal to</option>
    </select></td>
    <td width="159">
    <input name="bed" type="text" id="bed" size="2"
    maxlength="2">bed(s)</td>
    </tr>
    <tr>
    <td align="right">Price Range :</td>
    <td><select name="price_option" id="price_option">
    <option value="above" selected>Above</option>
    <option value="below">Below</option>
    </select></td>
    <td>$<input name="price" type="text" id="price" size="5"
    maxlength="5"></td>
    </tr>
    <tr>
    <td align="right">&nbsp;</td>
    <td colspan="2" align="center"><input type="submit" name="Submit" value="Search">
    </td>
    </tr>
    </table>
    </form>
    <?php
    mysql_free_result($rsSearch);
    ?>
    <table width="83%" border="2" align="center" bordercolor="#0000FF">
    <tr>
    <td width="8%"><h4>ID</h4></td>
    <td width="14%"><h4>Price</h4></td>
    <td width="9%"><h4>Bed</h4></td>
    <td width="51%"><h4>Description</h4></td>
    <td width="18%"><h4>Number</h4></td>
    </tr>
    <?php do { ?>
    <tr>
    <td><?php echo $row_rsSearch['ID']; ?></td>
    <td><?php echo $row_rsSearch['price']; ?></td>
    <td><?php echo $row_rsSearch['bed']; ?></td>
    <td><?php echo $row_rsSearch['description']; ?></td>
    <td><?php echo $row_rsSearch['number']; ?></td>
    </tr>
    <?php } while ($row_rsSearch = mysql_fetch_assoc($rsSearch)); ?>
    </table>
    Last edited by TheCut; 02-04-2003 at 01:52 AM.

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Looks to me as if your SQL query is correct, but doesn't find anything, i.e. it does not return any rows that matched the search criteria. You should test if there are any rows in the result set (a simple if statement added) and only if there are any, call mysql_fetch_assoc().

  • #3
    Registered User
    Join Date
    Feb 2003
    Posts
    43
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hey

    Your code is too messy

    and I'm too lazy

    check out php.net and see what function you are using.. and whether you are storing in an array.

    Jesh

  • #4
    New Coder
    Join Date
    Jan 2003
    Location
    U.S.A
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi folks,

    I have tried to edit the code above using DreamWeaverMX, but it did not help me much. I run into the same error as above everytime I test the code. Actually, it returned the result one record at the time. but what I try to figure out is how to make the query return all the records in the database that matchs the user search criteria.

    Anyone has other ideas of modify the code of above, or knowing where can I find the sample php & mysql script for the search engine. I'm trying to accomplish searching option code for the "Room" table, which include these fields {ID, Price, Bed, Description, and Number}

    By the way, can you folks show me how to post PHP code in this forums, to make it easier to read. Thank you very much.

    TheCut


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •