0

I was trying to loop through my database for some results and recieved this error:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /hermes/waloraweb032/b867/moo.classiadsinfo/ajax/searchApp.php on line 8

and heres my code:

<?php
require_once '../connect.php';

if(isset($_POST['search_term'])==true && empty($_POST['search_term'])==false)
{
$search_term=mysql_real_escape_string($_POST['search_term']);
$query=mysql_query("SELECT `Category`, `Type` FROM `autosuggest` WHERE `Category=.'$_POST[category]'` && `Type` LIKE '$search_term%'");
while(($row=mysql_fetch_assoc($query))!==false)
{
  echo '<li><b>',$row['Type'],'</b></li>';
}

}
?> 

please help!!!

4
Contributors
51
Replies
53
Views
4 Years
Discussion Span
Last Post by broj1
0

The $query variable does not contain a valid resource since there is an error in the query. You can not perform a concatenation within a double quoted string. Correct the query this way: remove the . and enclose the array element $_POST[category] in curly braces). I also changed && to AND since I am not sure whether && is valid in mysql. Also a backtick was missing after the Category.

$query=mysql_query("SELECT `Category`, `Type` FROM `autosuggest` WHERE `Category`='{$_POST[category]}'` AND `Type` LIKE '$search_term%'");

The name of the $query variable is missleading since it contains a resource not a query. I would change it to $resource.

$resource=mysql_query("SELECT `Category`, `Type` FROM `autosuggest` WHERE `Category`='{$_POST[category]}'` AND `Type` LIKE '$search_term%'");

while($row=mysql_fetch_assoc($resource))
{
    echo '<li><b>',$row['Type'],'</b></li>';
}

And as bradly.spicer said switch to newer mysqli instead of older mysql, when possible.

Edited by broj1

0
<?php
require_once '../connect.php';

if(isset($_POST['search_term'])==true && empty($_POST['search_term'])==false)
{
$search_term=mysql_real_escape_string($_POST['search_term']);
 $resource=mysql_query("SELECT `Category`, `Type` FROM `autosuggest` WHERE `Category`='{$_POST[category]}'` AND `Type` LIKE '$search_term%'");

while(($row=mysql_fetch_assoc($resource))!==false)
{
echo '<li><b>',$row['Type'],'</b></li>';
}

}
?> 

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /hermes/waloraweb032/b867/moo.classiadsinfo/ajax/searchApp.php on line 11

0

Looks like there is still an error in the query. Can you add this line of code just after line 6:

    die("SELECT `Category`, `Type` FROM `autosuggest` WHERE `Category`='{$_POST[category]}'` AND `Type` LIKE '$search_term%'");

As you see it will display the constructed query. Can you please post it here.

You can also copy the displayed query and test it in phpmyadmin.

Edited by broj1

0
SELECT `Category`, `Type` FROM `autosuggest` WHERE `Category`=''` AND `Type` LIKE 's%

tht was displayed

Edited by DamzWildfire

0

There is a backtick just before the AND that should not be there. Remove it (and comment out the die statement) then try it out.

0

Yo check for search term before issuing the query but you do not check for Category. Is this OK? Is category not mandatory to have a value?

if(isset($_POST['search_term'])==true && empty($_POST['search_term'])==false)
0

how this should work is i have a drop down with different categories onece a category is select theres a type text box with an autosuggest feature attached. so when a category is seletect only the keywords associated with that category should be shown when typed

0

But category is obviously not in the $_POST. Can you put this line in the very beginning of the script and post the result:

die(print_r($_POST, 1));

This will print the contents of the $_POST array and stop the script.

Edited by broj1

0

OK, so the category is not comming through. Can you post the code for the form (to check the select).

0
 <form action="SearchAppliance.php" method="post" name="search1" onsubmit='return validateForm()'>

   <table width="685" border="0">
  <tr>
       <td width="238" height="23" valign="top">
  <font size="-2">Search</font><font size="-1"><strong> APPLIANCES </strong></font>

       </td>
       <td width="443" valign="top">
       <input type="submit" value="Find" id="findbuton">
       </td>
       </tr>
       </table>
       <div id="dropdown">
       <table width="621" border="0" align="center">
       <tr><td width="100">

        <span>Category</span><br />
            <select name="category" style="width: ; background-color: #FFF; font-weight: bold; font-size: 12px; width: 120px;" ONCHANGE="setup(document.search1.category.value)">

              <option value="category" selected="selected">All</option>
              <option value="cleaning">Cleaning</option>
              <option value="cooling">Cooling</option>
              <option value="heating">Heating</option>
              <option value="kitchen">Kicthen</option>
              <option value="lighting">Lighting</option>
              <option value="washroom">Washroom</option>
            </select></td>
  <td width="107">
  <span>Type:</span><br />
   <script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
  <script src="primaryapp.js"></script>
      <input type="text" name="tag" value="" placeholder="All" class="autosuggest"/>
      <div class="dropdown">
      <ul class="result">

      </ul>

      </div>
     </td>
     <td width="120">
     <span>Location</span><br />
        <select style="width: 120px; background-color: #FFF; font-weight: bold; font-size: 12px;" name="parish">
          <option selected="selected">All</option>
          <option>Clarendon</option>
          <option>Hanover</option>
          <option>Kingston</option>
          <option>Manchester</option>
          <option>Portland</option>
          <option>Trelawny</option>
          <option>Westmoreland</option>
          <option>St. Andrew</option>
          <option>St. Ann</option>
          <option>St. Catherine</option>
          <option>St. Elizabeth</option>
          <option>St. James</option>
          <option>St. Mary</option>
          <option>St. Thomas</option> 
        </select></td>
 <td width="120"> Condition<br /> 
        <select style="width: 120px; background-color: #FFF; font-weight: bold; font-size: 12px;" name="condition">
          <option>All</option>
          <option>New</option>
          <option>Used</option>
        </select>
    </td>
    <td width="162">Sort by<br />
         <select style="width: 120px; background-color: #FFF; font-weight: bold; font-size: 12px;" name="condition">
          <option selected="selected">All</option>
          <option>Brand</option>
          <option>Conditon</option>
          <option>Item status</option>
          <option>Location</option>
          <option>Price</option>
          <option>Type</option>
          <option>Voltage</option>

        </select>
        </td>
        <SPAN ID="rep"></SPAN></table> 
   </div>

</form></td>
Form code

Edited by DamzWildfire

0

Thing is this code i gets the info from a javascript file that contains a few lines of jquery. in tht jquery it gets the value of what was typed and pass it to this code below. so im thinking i should do the same fr the dropdown and pass it tot his code what you think?

<?php
//die(print_r($_POST, 1));
require_once '../connect.php';
if(isset($_POST['search_term'])==true && empty($_POST['search_term'])==false)
{
$search_term=mysql_real_escape_string($_POST['search_term']);
 $resource=mysql_query("SELECT `Type` FROM `autosuggest` WHERE `Type` LIKE '$search_term%'");
 while(($row=mysql_fetch_assoc($resource))!==false)
{
echo '<li><b>',$row['Type'],'</b></li>';
}

}
0

If I understand the thing after revising your code you wish to have a functionality that will display suggestions upon typing in an input box and based on selection in dropdowns. This is quite a complex task which involves ajax calls fired on each keypress while you are in the input box. jquery has functionalities ready for that but still you have to do customization since you have additional selections.

The javascript logic is not in the posted code and I guess it is in the primaryapp.js, which is included with script tags. But this case is quite hard to debug since all the javascript should be posted, the db tables should be posted (structure and contents) and the php and html scripts should be examined. I can not promise you that I can do that quickly.

Meanwhile see here for nice tutorials how to do the autosuggest feature.

I hope someone with experience with this will be also able to contribute.

0
primary.js

// JavaScript Document
$(document).ready(function(){
$('.autosuggest').keyup(function (){
    var search_term=$(this).attr('value');
    //var category=$(this).attr('value');
    $.post('ajax/searchApp.php',{search_term:search_term},function(data){   
        //alert(data);
        $('.result').html(data);
        $('.result li').click(function()
        {
            var result_value=$(this).text();
            $('.autosuggest').attr('value',result_value);
            //alert(result_value);
            $('.result').html('');

            });
        });
    });

});// JavaScript Document
0
select.js this i was hoping would get the dropdown value from the form and pass it back to the php file

jQuery(function($){
    $("#cateory").change(function(){
        var selected_value = $(this).val();
        /*AJAX CODE HERE TO PASS VALUE OF DROP DOWN TO PHP*/
        $.post("searchApp.php", $("#category").serialize()); //this is the problematic part: it needs to pass the $currency variable
    })

}
0

table has 8 columns inlcuding category and type. category refering to the the drop down menu and type refring to what was entered in the autoseggest feild

0

In line 4 in above snippet you misspelled category (you left out letter g):

$("#cateory").change(function(){
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.