vindesigns Posted August 19, 2011 Share Posted August 19, 2011 Hi all, I am having trouble in getting my search facility to work on a web server. Currently when I run the website on my local server using WAMP it works fine and retrieves all the results contained in the database but it does not retrieve any results when uploaded to a web server. I'm currently using Heart Internet if this helps. Below is the code I have used for the search facility: <?php // this script searches for matches on the posted search string in courses, modules tables. $searchstr = $_POST['search']; $trimmedstr = trim($searchstr); //trim whitespace from the stored variable echo "<h2 id='homecol'>Searching for... "".$trimmedstr.""</h2><hr class='homecols' />"; //Build SQL search queries //search courses table $queryProd = "SELECT * FROM courses WHERE courseTitle LIKE \"%$trimmedstr%\" OR courseDescription LIKE \"%$trimmedstr%\" OR courseCode LIKE \"%$trimmedstr%\"ORDER BY courseTitle"; $numresultsProd=mysql_query($queryProd); $numrowsProd=mysql_num_rows($numresultsProd); // echo "results found for courses: ".$numrowsProd."<br>"; // test code //search modules table $queryCat = "SELECT * FROM modules WHERE moduleTitle LIKE \"%$trimmedstr%\" OR moduleSummary LIKE \"%$trimmedstr%\" OR moduleCode LIKE \"%$trimmedstr%\"ORDER BY moduleTitle"; $numresultsCat=mysql_query($queryCat); $numrowsCat=mysql_num_rows($numresultsCat); // echo "results found for modules: ".$numrowsCat."<br>"; //test code // If no results found, offer google search as alternative or back to home page if ($numrowsProd + $numrowsCat == 0) { echo "<h3>Results:</h3><br>"; echo "<p>Sorry, we could not find any results for: "".$trimmedstr.""</p>"; // google echo "<p><a href=\"http://www.google.com/search?q=".$trimmedstr."\" target=\"_blank\" title=\"Look up".$trimmedstr." on Google\"> Click here to search on google instead</a> or Return to our <a href='../home.php'>Home Page</a></p>"; } else //list search results { if($numrowsProd >0) // list results fromcourses { $resultProd = mysql_query($queryProd) or die("Couldn't execute query"); // begin ordered list echo "<h3><strong>Results from Courses:</strong></h3><p><ol>"; $countProd = 1; // display the results returned while ($rowProd= mysql_fetch_array($resultProd)) { $courseCode = $rowProd["courseCode"]; $title = $rowProd["courseTitle"]; $prodID = $rowProd["courseID"]; $descProd = $rowProd["courseDescription"]; echo "<p><li><a href='../department/courseselect.php?pid=$prodID'>$title</a> - $descProd</li></p>"; $countProd++ ; } echo "</ol></p>"; } if($numrowsCat >0) // and/or list results from modules { $resultCat = mysql_query($queryCat) or die("Couldn't execute query"); // begin ordered list echo " <h3>Results from Modules:</h3><p><ol>"; $countCat = 1; // display the results returned while ($rowCat= mysql_fetch_array($resultCat)) { $moduleCode = $rowCat["moduleCode"]; $catName = $rowCat["moduleTitle"]; $catID = $rowCat["moduleID"]; $descCat = $rowCat["moduleSummary"]; echo "<p><li><a href='../department/module_detail.php?pid=$catID'>$catName</a> - $descCat</li></p>"; $countCat++ ; } echo "</ol></p>"; } echo "<p>Please click on either the course or module to view further information</p>"; } ?> If anyone has any ideas please let me know. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/245199-search-facility-not-working-on-webserver/ Share on other sites More sharing options...
vindesigns Posted August 20, 2011 Author Share Posted August 20, 2011 I would greatly appreciate any suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/245199-search-facility-not-working-on-webserver/#findComment-1259738 Share on other sites More sharing options...
titan21 Posted August 20, 2011 Share Posted August 20, 2011 Are you able to access your logs to see if there are any clues there? First order of business! Quote Link to comment https://forums.phpfreaks.com/topic/245199-search-facility-not-working-on-webserver/#findComment-1259742 Share on other sites More sharing options...
vindesigns Posted September 6, 2011 Author Share Posted September 6, 2011 I have looked at the php log file from the web server and I get the following errors: PHP Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in includes/search.php on line 115 PHP Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in includes/search.php on line 121 Any ideas anyone what it might be? [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/245199-search-facility-not-working-on-webserver/#findComment-1266063 Share on other sites More sharing options...
jasonc Posted September 6, 2011 Share Posted September 6, 2011 Hey, We need to find out what the queries are in lines 113 and 119 of your original search.php file. First make a backup of your original search.php file then copy the below and replace with your original file. This should echo out the queries that your script is trying to use in the above lines. Reply with these line so we can better help you. <?php require_once('../Connections/module_system.php'); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"><!-- InstanceBegin template="/Templates/frontend.dwt" codeOutsideHTMLIsLocked="false" --> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <!-- InstanceBeginEditable name="doctitle" --> <title>Untitled Document</title> <link href="../admin/css/tabs.css" rel="stylesheet" type="text/css" /> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script> <script type="text/javascript"> $(document).ready(function() { //Default Action $(".tab_content").hide(); //Hide all content $("ul.tabs li:first").addClass("active").show(); //Activate first tab $(".tab_content:first").show(); //Show first tab content //On Click Event $("ul.tabs li").click(function() { $("ul.tabs li").removeClass("active"); //Remove any "active" class $(this).addClass("active"); //Add "active" class to selected tab $(".tab_content").hide(); //Hide all tab content var activeTab = $(this).find("a").attr("href"); //Find the rel attribute value to identify the active tab + content $(activeTab).fadeIn(); //Fade in the active content return false; }); }); </script> <!-- InstanceEndEditable --> <!-- InstanceBeginEditable name="head" --> <link href="../css/frontend.css" rel="stylesheet" type="text/css" /><!--[if lte IE 7]> <style> .content { margin-right: -1px; } /* this 1px negative margin can be placed on any of the columns in this layout with the same corrective effect. */ ul.nav a { zoom: 1; } /* the zoom property gives IE the hasLayout trigger it needs to correct extra whiltespace between the links */ </style> <![endif]--> <style type="text/css"> body { background-color: #FFF; } </style> <!-- InstanceEndEditable --> <link href="../css/frontend.css" rel="stylesheet" type="text/css" /><!--[if lte IE 7]> <style> .content { margin-right: -1px; } /* this 1px negative margin can be placed on any of the columns in this layout with the same corrective effect. */ ul.nav a { zoom: 1; } /* the zoom property gives IE the hasLayout trigger it needs to correct extra whiltespace between the links */ </style> <![endif]--> <style type="text/css"> body { background-color: #FFF; } </style> </head> <body> <!-- InstanceBeginEditable name="EditRegion3" --> <div class="container2"></div> <div class="header2"><span class="header"><img src="../images/edgehill_logo_long.png" width="1030" height="115" alt="edgehill logo" /></span> <div id="search"> <form id="form1" name="form1" method="post" action="search.php"> <label for="search">Search </label> <input type="text" name="search" id="search" /><input type="submit" name="searchgo" id="searchgo" value="Go" /> </form> </div> </div> <div id="breadcrumb_left"><a href="../index.php">Home</a> > Search</div> <div class="container"> <?php include("fronend_navbar.php"); ?> <div class="content"> <ul class="tabs"> <li><a href="#tab1">Search Results</a></li> </ul> <div class="tab_container"> <div id="tab1" class="tab_content"> <form method="post" name="form2" id="form2"> <div id="results-table"> <table width="200" border="1"> <tr> <td><?php // this script searches for matches on the posted search string in courses, modules tables. $searchstr = $_POST['search']; $trimmedstr = trim($searchstr); //trim whitespace from the stored variable echo "<h2 id='homecol'>Searching for... "".$trimmedstr.""</h2><hr class='homecols' />"; //Build SQL search queries //search courses table $queryProd = "SELECT * FROM courses WHERE courseTitle LIKE \"%$trimmedstr%\" OR courseDescription LIKE \"%$trimmedstr%\" OR courseCode LIKE \"%$trimmedstr%\"ORDER BY courseTitle"; echo("<br><br>" . $queryProd . "<br><br>"); $numresultsProd=mysql_query($queryProd); $numrowsProd=mysql_num_rows($numresultsProd); // echo "results found for courses: ".$numrowsProd."<br>"; // test code //search modules table $queryCat = "SELECT * FROM modules WHERE moduleTitle LIKE \"%$trimmedstr%\" OR moduleSummary LIKE \"%$trimmedstr%\" OR moduleCode LIKE \"%$trimmedstr%\"ORDER BY moduleTitle"; echo("<br><br>" . $queryCat . "<br><br>"); $numresultsCat=mysql_query($queryCat); $numrowsCat=mysql_num_rows($numresultsCat); // echo "results found for modules: ".$numrowsCat."<br>"; //test code // If no results found, offer google search as alternative or back to home page if ($numrowsProd + $numrowsCat == 0) { echo "<h3>Results:</h3><br>"; echo "<p>Sorry, we could not find any results for: "".$trimmedstr.""</p>"; // google echo "<p><a href=\"http://www.google.com/search?q=".$trimmedstr."\" target=\"_blank\" title=\"Look up".$trimmedstr." on Google\"> Click here to search on google instead</a> or Return to our <a href='../home.php'>Home Page</a></p>"; } else //list search results { if($numrowsProd >0) // list results fromcourses { $resultProd = mysql_query($queryProd) or die("Couldn't execute query"); // begin ordered list echo "<h3><strong>Results from Courses:</strong></h3><p><ol>"; $countProd = 1; // display the results returned while ($rowProd= mysql_fetch_array($resultProd)) { $courseCode = $rowProd["courseCode"]; $title = $rowProd["courseTitle"]; $prodID = $rowProd["courseID"]; $descProd = $rowProd["courseDescription"]; echo "<p><li><a href='../department/courseselect.php?pid=$prodID'>$title</a> - $descProd</li></p>"; $countProd++ ; } echo "</ol></p>"; } if($numrowsCat >0) // and/or list results from modules { $resultCat = mysql_query($queryCat) or die("Couldn't execute query"); // begin ordered list echo " <h3>Results from Modules:</h3><p><ol>"; $countCat = 1; // display the results returned while ($rowCat= mysql_fetch_array($resultCat)) { $moduleCode = $rowCat["moduleCode"]; $catName = $rowCat["moduleTitle"]; $catID = $rowCat["moduleID"]; $descCat = $rowCat["moduleSummary"]; echo "<p><li><a href='../department/module_detail.php?pid=$catID'>$catName</a> - $descCat</li></p>"; $countCat++ ; } echo "</ol></p>"; } echo "<p>Please click on either the course or module to view further information</p>"; } ?></td> </tr> <tr> </tr> </table> <p> </p> <p> </p> </div> </form> </div> </div> <p> </p> </div> <!-- end .content --> </div> <div class="footer2"> <div class="footer"> <p><b>© 2011 Edge Hill University, St Helens Road, Ormskirk, Lancashire, L39 4QP, UK</b></p> <p id="footorcol"><a href="http://www.edgehill.ac.uk/governance/charity">Contact Us</a> | <a href="../help.php">Help</a></p> </div> </div> <!-- InstanceEndEditable --> </body> <!-- InstanceEnd --></html> Quote Link to comment https://forums.phpfreaks.com/topic/245199-search-facility-not-working-on-webserver/#findComment-1266220 Share on other sites More sharing options...
vindesigns Posted September 7, 2011 Author Share Posted September 7, 2011 Hi, I have used the code and now when I search for example module code 'ac1001' its comes up with the sql statements: SELECT * FROM courses WHERE courseTitle LIKE \"%$trimmedstr%\" OR courseDescription LIKE \"%$trimmedstr%\" OR courseCode LIKE \"%$trimmedstr%\"ORDER BY courseTitle SELECT * FROM modules WHERE moduleTitle LIKE \"%$trimmedstr%\" OR moduleSummary LIKE \"%$trimmedstr%\" OR moduleCode LIKE \"%$trimmedstr%\"ORDER BY moduleTitle I got the following errors from the errors log: [Wed Sep 07 11:36:55 2011] [error] PHP Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in search.php on line 118 [Wed Sep 07 11:36:55 2011] [error] PHP Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in search.php on line 127 Quote Link to comment https://forums.phpfreaks.com/topic/245199-search-facility-not-working-on-webserver/#findComment-1266369 Share on other sites More sharing options...
PFMaBiSmAd Posted September 7, 2011 Share Posted September 7, 2011 If you search for that error message, you will find that it generally means that your query failed due to an error of some kind. For debugging purposes, use mysql_error() in an or die() statement to get your code to tell you why - $numresultsProd=mysql_query($queryProd) or die(mysql_error()); $numresultsCat=mysql_query($queryCat) or die(mysql_error()); I'll bet it is something to do with your database connection or a nonexistent database table. Quote Link to comment https://forums.phpfreaks.com/topic/245199-search-facility-not-working-on-webserver/#findComment-1266383 Share on other sites More sharing options...
jasonc Posted September 7, 2011 Share Posted September 7, 2011 ok let try the following code, all i have done is comment out the lines that perform the query and added an 'exit;' to the end of the echo of the last query. Copy the text that you get between the "astrix's" *** text *** <?php require_once('../Connections/module_system.php'); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"><!-- InstanceBegin template="/Templates/frontend.dwt" codeOutsideHTMLIsLocked="false" --> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <!-- InstanceBeginEditable name="doctitle" --> <title>Untitled Document</title> <link href="../admin/css/tabs.css" rel="stylesheet" type="text/css" /> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script> <script type="text/javascript"> $(document).ready(function() { //Default Action $(".tab_content").hide(); //Hide all content $("ul.tabs li:first").addClass("active").show(); //Activate first tab $(".tab_content:first").show(); //Show first tab content //On Click Event $("ul.tabs li").click(function() { $("ul.tabs li").removeClass("active"); //Remove any "active" class $(this).addClass("active"); //Add "active" class to selected tab $(".tab_content").hide(); //Hide all tab content var activeTab = $(this).find("a").attr("href"); //Find the rel attribute value to identify the active tab + content $(activeTab).fadeIn(); //Fade in the active content return false; }); }); </script> <!-- InstanceEndEditable --> <!-- InstanceBeginEditable name="head" --> <link href="../css/frontend.css" rel="stylesheet" type="text/css" /><!--[if lte IE 7]> <style> .content { margin-right: -1px; } /* this 1px negative margin can be placed on any of the columns in this layout with the same corrective effect. */ ul.nav a { zoom: 1; } /* the zoom property gives IE the hasLayout trigger it needs to correct extra whiltespace between the links */ </style> <![endif]--> <style type="text/css"> body { background-color: #FFF; } </style> <!-- InstanceEndEditable --> <link href="../css/frontend.css" rel="stylesheet" type="text/css" /><!--[if lte IE 7]> <style> .content { margin-right: -1px; } /* this 1px negative margin can be placed on any of the columns in this layout with the same corrective effect. */ ul.nav a { zoom: 1; } /* the zoom property gives IE the hasLayout trigger it needs to correct extra whiltespace between the links */ </style> <![endif]--> <style type="text/css"> body { background-color: #FFF; } </style> </head> <body> <!-- InstanceBeginEditable name="EditRegion3" --> <div class="container2"></div> <div class="header2"><span class="header"><img src="../images/edgehill_logo_long.png" width="1030" height="115" alt="edgehill logo" /></span> <div id="search"> <form id="form1" name="form1" method="post" action="search.php"> <label for="search">Search </label> <input type="text" name="search" id="search" /><input type="submit" name="searchgo" id="searchgo" value="Go" /> </form> </div> </div> <div id="breadcrumb_left"><a href="../index.php">Home</a> > Search</div> <div class="container"> <?php include("fronend_navbar.php"); ?> <div class="content"> <ul class="tabs"> <li><a href="#tab1">Search Results</a></li> </ul> <div class="tab_container"> <div id="tab1" class="tab_content"> <form method="post" name="form2" id="form2"> <div id="results-table"> <table width="200" border="1"> <tr> <td><?php // this script searches for matches on the posted search string in courses, modules tables. $searchstr = $_POST['search']; $trimmedstr = trim($searchstr); //trim whitespace from the stored variable echo "<h2 id='homecol'>Searching for... "".$trimmedstr.""</h2><hr class='homecols' />"; //Build SQL search queries //search courses table $queryProd = "SELECT * FROM courses WHERE courseTitle LIKE \"%$trimmedstr%\" OR courseDescription LIKE \"%$trimmedstr%\" OR courseCode LIKE \"%$trimmedstr%\"ORDER BY courseTitle"; echo("***<br><br>" . $queryProd . "<br><br>"); $numresultsProd=mysql_query($queryProd); $numrowsProd=mysql_num_rows($numresultsProd); // echo "results found for courses: ".$numrowsProd."<br>"; // test code //search modules table $queryCat = "SELECT * FROM modules WHERE moduleTitle LIKE \"%$trimmedstr%\" OR moduleSummary LIKE \"%$trimmedstr%\" OR moduleCode LIKE \"%$trimmedstr%\"ORDER BY moduleTitle"; echo("<br><br>" . $queryCat . "<br><br>***"); $numresultsCat=mysql_query($queryCat); $numrowsCat=mysql_num_rows($numresultsCat); // echo "results found for modules: ".$numrowsCat."<br>"; //test code // If no results found, offer google search as alternative or back to home page if ($numrowsProd + $numrowsCat == 0) { echo "<h3>Results:</h3><br>"; echo "<p>Sorry, we could not find any results for: "".$trimmedstr.""</p>"; // google echo "<p><a href=\"http://www.google.com/search?q=".$trimmedstr."\" target=\"_blank\" title=\"Look up".$trimmedstr." on Google\"> Click here to search on google instead</a> or Return to our <a href='../home.php'>Home Page</a></p>"; } else //list search results { if($numrowsProd >0) // list results fromcourses { $resultProd = mysql_query($queryProd) or die("Couldn't execute query"); // begin ordered list echo "<h3><strong>Results from Courses:</strong></h3><p><ol>"; $countProd = 1; // display the results returned while ($rowProd= mysql_fetch_array($resultProd)) { $courseCode = $rowProd["courseCode"]; $title = $rowProd["courseTitle"]; $prodID = $rowProd["courseID"]; $descProd = $rowProd["courseDescription"]; echo "<p><li><a href='../department/courseselect.php?pid=$prodID'>$title</a> - $descProd</li></p>"; $countProd++ ; } echo "</ol></p>"; } if($numrowsCat >0) // and/or list results from modules { $resultCat = mysql_query($queryCat) or die("Couldn't execute query"); // begin ordered list echo " <h3>Results from Modules:</h3><p><ol>"; $countCat = 1; // display the results returned while ($rowCat= mysql_fetch_array($resultCat)) { $moduleCode = $rowCat["moduleCode"]; $catName = $rowCat["moduleTitle"]; $catID = $rowCat["moduleID"]; $descCat = $rowCat["moduleSummary"]; echo "<p><li><a href='../department/module_detail.php?pid=$catID'>$catName</a> - $descCat</li></p>"; $countCat++ ; } echo "</ol></p>"; } echo "<p>Please click on either the course or module to view further information</p>"; } ?></td> </tr> <tr> </tr> </table> <p> </p> <p> </p> </div> </form> </div> </div> <p> </p> </div> <!-- end .content --> </div> <div class="footer2"> <div class="footer"> <p><b>© 2011 Edge Hill University, St Helens Road, Ormskirk, Lancashire, L39 4QP, UK</b></p> <p id="footorcol"><a href="http://www.edgehill.ac.uk/governance/charity">Contact Us</a> | <a href="../help.php">Help</a></p> </div> </div> <!-- InstanceEndEditable --> </body> <!-- InstanceEnd --></html> Quote Link to comment https://forums.phpfreaks.com/topic/245199-search-facility-not-working-on-webserver/#findComment-1266389 Share on other sites More sharing options...
vindesigns Posted September 7, 2011 Author Share Posted September 7, 2011 Hi all, PFMaBiSmAd you were write about the database tables not being selected. I selected both course and module tables and it worked, all results now come through. I would like to thank you both for helping me out. What I don't understand is that why did the results show up when I was testing it on my local machine using wamp without no tables being selected? Also in the code at the moment, if the user does not enter any criteria and searches, it retrieves back a list of all courses and modules stored in the database. What do I need to put in order if a user searched without no keywords or phrases, I would like it to say something like 'please enter a search phrase'? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/245199-search-facility-not-working-on-webserver/#findComment-1266399 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.