webguync Posted May 13, 2011 Share Posted May 13, 2011 I am trying out some code I got from a tutorial and it's not working for me. The code is for a form and when you type in any letter or string that matches against the DB content it is supposed to display. I only get the 'No Matches Found ' string as a result. No DB errors or anything. Also I echoed out the resulting SQL and when I enter that into PHPMyAdmin I come up with results. I am hoping it's just something obvious i am overlooking. Also I am using JQuery and AJAX, but this isn't causing it not to work b/c I tested without the JQuery and still get the same result. code posted below <html> <head> <script language="javascript" type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.0/jquery.min.js" /> </script> <script type='text/javascript'> $(document).ready(function(){ $("#search_results").slideUp(); $("#search_button").click(function(e){ e.preventDefault(); ajax_search(); }); $("#search_term").keyup(function(e){ e.preventDefault(); ajax_search(); }); }); function ajax_search(){ $("#search_results").show(); var search_val=$("#search_term").val(); $.post("./find.php", {search_term : search_val}, function(data){ if (data.length>0){ $("#search_results").html(data); } }) } </script> <meta http-equiv="Content-Type" content="text/html; charset=iso- 8859-1" /> <title>Novo RPC Results Search Engine</title> <link href="default.css" rel="stylesheet" type="text/css" media="screen" /> </head> <body> <h1>Search our Phone Directory</h1> <form id="searchform" method="post" action="find.php"> <div> <label for="search_term">Search name/phone</label> <input type="text" name="search_term" id="search_term" /> <input type="submit" value="search" id="search_button" /> </div> </form> <div id="search_results"></div> </body> </html> the PHP stuff <?php define(HOST, "localhost"); define(USER, "username"); define(PW, "pw"); define(DB, "DB_Name"); $connect = mysql_connect(HOST,USER,PW) or die('Could not connect to mysql server.' ); mysql_select_db(DB, $connect) or die('Could not select database.'); $term = strip_tags(substr($_POST['search_term'],0, 100)); $term = mysql_escape_string($term); $sql = "select name,phone from directory where name like '%$term%' or phone like '%$term%' order by name asc"; $string = ''; if (mysql_num_rows($result) > 0){ while($row = mysql_fetch_object($result)){ $string .= "<b>".$row->name."</b> - "; $string .= $row->phone."</a>"; $string .= "<br/>\n"; } }else{ $string = "No matches found!"; } echo $string; echo $sql; ?> and the DB code CREATE TABLE `directory` ( `id` int(11) NOT NULL auto_increment, `name` varchar(64) NOT NULL, `phone` varchar(16) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 AUTO_INCREMENT=6 ; -- -- Dumping data for table `directory` -- INSERT INTO `directory` (`id`, `name`, `phone`) VALUES (1, 'Tom Smith', '512-555-0111'), (2, 'Bill Smith', '512-555-0112'), (3, 'John Smith', '512-555-0113'), (4, 'Jane Smith', '512-555-0114'), (5, 'Sara Smith', '512-555-0115'); Link to comment https://forums.phpfreaks.com/topic/236347-form-to-search-db-content-not-working-correctly/ Share on other sites More sharing options...
PFMaBiSmAd Posted May 13, 2011 Share Posted May 13, 2011 You are not executing the query in your code, so it will be a little hard for there to be any matching results. Link to comment https://forums.phpfreaks.com/topic/236347-form-to-search-db-content-not-working-correctly/#findComment-1215133 Share on other sites More sharing options...
webguync Posted May 13, 2011 Author Share Posted May 13, 2011 I thought this part of the code did that. Guess not huh? if (mysql_num_rows($result) > 0){ while($row = mysql_fetch_object($result)){ $string .= "<b>".$row->name."</b> - "; $string .= $row->phone."</a>"; $string .= "<br/>\n"; } Link to comment https://forums.phpfreaks.com/topic/236347-form-to-search-db-content-not-working-correctly/#findComment-1215141 Share on other sites More sharing options...
Zane Posted May 14, 2011 Share Posted May 14, 2011 I thought this part of the code did that. Guess not huh? if (mysql_num_rows($result) > 0){ while($row = mysql_fetch_object($result)){ $string .= "".$row->name." - "; $string .= $row->phone.""; $string .= " \n"; } You need to execute the query.... $result = mysql_query($sql); /// This is the execution if (mysql_num_rows($result) > 0){ while($row = mysql_fetch_object($result)){ $string .= "".$row->name." - "; $string .= $row->phone.""; $string .= " \n"; } Link to comment https://forums.phpfreaks.com/topic/236347-form-to-search-db-content-not-working-correctly/#findComment-1215321 Share on other sites More sharing options...
webguync Posted May 14, 2011 Author Share Posted May 14, 2011 that makes sense, thanks! Link to comment https://forums.phpfreaks.com/topic/236347-form-to-search-db-content-not-working-correctly/#findComment-1215433 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.