I need help with this program

0 Kumi Festus · October 31, 2014
I want to query a database to display search queries instantly.
This is the warning message:mysql_fetch_assoc() expects parameter 1 to be resource, integer 

See The Code Below:
index.php
<form action="index.php" method="post">
Start Searching<input type="text" name="search_term" id="search" size="60"/>
<span id="search_results"></span>
</form>


The Script

$(document).ready(function(){
    $("#search").keyup(function(){
    var search_term = $(this).val();
    $.post('search.php',{search_term:search_term},function(data){
    $("#search_results").text(data);
    });
 });
});

search.php

<?php
require 'db.php';
if(isset($_POST["search_term"])){
$search_term = mysql_real_escape_string(htmlentities($_POST["search_term"]));
 if(!empty($search_term)){
    $search = mysql_query("SELECT place,description FROM tbl1 WHERE place LIKE '%$search_term%'");
    $result_count = mysql_num_rows($search);
    $suffix = ($result_count !=1) ? 's':'';
    while($results_row = mysql_fetch_assoc($result_count)){
    echo "<p><strong>",$results_row['place'],"</strong><br/>",$results_row['description'],"</p>";
    }
 }
}
?>

db.php

<?php
mysql_connect("localhost","root","");
mysql_select_db("dtb1");
?>

I'm sure there is nothing wrong with code, well maybe.

Post a Reply

Replies

Oldest  Newest  Rating
+1 Ron Butcher · October 31, 2014
You are getting that message because it expects the argument passed to mysql_fetch_assoc to be the query resource, not the number of rows.

Change the while loop to this:


while($results_row = mysql_fetch_assoc($search))


One more thing, you should start looking at mysqli (MySQL Enhanced) or PDO instead of mysql.  That extension has been depreciated and is going to be removed from PHP all together pretty soon.
0 Kumi Festus · November 1, 2014
Thanks a lot. I figured that out later.

I will look into your suggestion, Thanks
0 Kumi Festus · November 2, 2014
I am testing this program on my local computer bundled with XAMPP. Is it possible to switch on the local computer?
0 Kumi Festus · November 2, 2014
I means this is not a program I am going to upload on a live server. I am learning now and this database is on XAMPP.
0 Ron Butcher · November 2, 2014
Yes you can use mysqli or PDO on your computer with XAMPP.  It is pretty simple really, here is a link to get you started:  http://php.net/manual/en/mysqli.quickstart.dual-interface.php.  

If you want to continue using the procedural way, the commands are almost the exact same, with a few differences.  
0 Kumi Festus · November 3, 2014
Yeah,
Thanks a bunch.
They do really look similar.
I will use that from now on. Thanks
  • 1

PHP

129,807 followers
About

Server-side, HTML embedded scripting language used to create dynamic Web pages.

Links
Moderators