merylvingien Posted September 9, 2010 Share Posted September 9, 2010 Hi folks, i am trying to impliment a ajax php search function to my site, but just stuck on how to prevent a undefined variable Here is thee ofending code: <?php $con = mysql_connect("localhost", "root", ""); if (!$con) { die('<p>There seems to be a problem, please try again soon.</p>'); } $db_selected = mysql_select_db("database",$con); //get the q parameter from URL $q=$_GET["q"]; //lookup all links from the xml file if length of q>0 $sql="SELECT * FROM simp_search WHERE `sbody` LIKE '%{$q}%' OR `varia` LIKE '%{$q}%' OR `stitle` LIKE '%{$q}%' ORDER BY `stitle`"; $result = mysql_query($sql); if (mysql_num_rows($result) < 1) { $error[] = "<p>Please try another search..."; }else { $results = array(); // the result array $i = 1; while ($row = mysql_fetch_assoc($result)) { $results[] = "{$i}:<a href=\"http://www.mysite/{$row['url']}\">{$row['stitle']}</a><br />{$row['sbody']}<br /><br />"; $i++; } } function removeEmpty($var) { return (!empty($var)); } if ($results > 0){$output = "<p>Your search term: " . $q . " returned:<br><br>" . implode("", $results); } else {$output= "No results found!";} echo "$output"; ?> At the moment its a mishmash of code from other applications. Works fine if you insert a search term that is in the database, however if you enter something that doesnt match, the output is: Notice: undefined variable: results in C:\www\website\livesearch.php on line 35 No results found! Any ideas on what to change to prevent this? Also i know there is some code in there that is not needed. Link to comment https://forums.phpfreaks.com/topic/213001-ajaxphp-search-app/ Share on other sites More sharing options...
fortnox007 Posted September 9, 2010 Share Posted September 9, 2010 I am really not sure on this one but maybe this works : ) $q=''; // i add this one $q=$_GET["q"]; Link to comment https://forums.phpfreaks.com/topic/213001-ajaxphp-search-app/#findComment-1109328 Share on other sites More sharing options...
merylvingien Posted September 9, 2010 Author Share Posted September 9, 2010 no, but thanks for the reply Link to comment https://forums.phpfreaks.com/topic/213001-ajaxphp-search-app/#findComment-1109335 Share on other sites More sharing options...
merylvingien Posted September 9, 2010 Author Share Posted September 9, 2010 Solved it, doh, if empty lol Link to comment https://forums.phpfreaks.com/topic/213001-ajaxphp-search-app/#findComment-1109341 Share on other sites More sharing options...
fortnox007 Posted September 9, 2010 Share Posted September 9, 2010 I think i found it : ) Your using $output without initializing it first outside the if statement. try this lol i hope this works $output='monkeys'; // just as an example function removeEmpty($var) { return (!empty($var)); } if ($results > 0){$output = "<p>Your search term: " . $q . " returned:<br><br>" . implode("", $results); } else {$output= "No results found!";} echo "$output"; Link to comment https://forums.phpfreaks.com/topic/213001-ajaxphp-search-app/#findComment-1109343 Share on other sites More sharing options...
fortnox007 Posted September 9, 2010 Share Posted September 9, 2010 lol nice one Link to comment https://forums.phpfreaks.com/topic/213001-ajaxphp-search-app/#findComment-1109344 Share on other sites More sharing options...
merylvingien Posted September 9, 2010 Author Share Posted September 9, 2010 FYI incase your interested the fix was this: if (empty($results)) {$output= "No results found!";} elseif ($results > 0){$output = "<p>Your search term: " . $q . " returned:<br><br>" . implode("", $results); } echo "$output"; Link to comment https://forums.phpfreaks.com/topic/213001-ajaxphp-search-app/#findComment-1109359 Share on other sites More sharing options...
fortnox007 Posted September 9, 2010 Share Posted September 9, 2010 : ) thx for sharing Link to comment https://forums.phpfreaks.com/topic/213001-ajaxphp-search-app/#findComment-1109374 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.