RIRedinPA Posted December 13, 2007 Share Posted December 13, 2007 I'm building a photo archive site for my company (we're a publisher). I'm working on the page navigation. Right now the site displays 18 results per page (6 to a row x 3 rows). If you get more than 18 results you obviously will need to navigate to see the other images. I'm trying to save the db query to a session variable so I can run the query and show the results that fit within the page your requesting - i.e. page 1 = results 1-18, 2 = results 19-36 and so on. I do a check to see what kind of search you're performing. If it's from the search form it is a new one and I parse the code to build the query and then before I run it I have this code: //save query to session $_SESSION['query'] = $query; which in this case is this: SELECT * FROM photorecord WHERE keywords LIKE '%school%' ORDER BY logNum DESC This returns 29 results. If you click on the next link to see the results > than item 18 I pass the search type as 'nav' and use this code to set the $query var. $query = $_SESSION['query']; which comes out to the same as the initial query. Whoohoo! Brilliant. Except. I am getting this error on my results page: You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 Your request couldn't be procesed. If I run the query through my mysql app (CocoaMySQL - I'm on a Mac) the query works fine. Also, if do a new search where the query changes, such as this: SELECT * FROM photorecord WHERE keywords LIKE '%nurse%' ORDER BY logNum DESC, and save that to the session, when I recall $_SESSION['query'] I get the old sql query where the keyword equaled 'school'. Any help would be appreciated. Here's the relevant part of my code. //get search type $searchType = $_REQUEST['search']; if ($searchType == "") { $searchType = "new"; } //do search based on type; if ($searchType == "new") { $keysToSearch = $_POST['keysToSearch']; $_SESSION['keysToSearch'] = $keysToSearch; $keyList = split( chr(32), $keysToSearch); if (count($keyList) < 2) { $query = "SELECT * FROM photorecord WHERE keywords LIKE '%" . $keyList[0] . "%'"; } else { for ($q=0; $q<=count($keyList); $q++) { if ($q == 0) { $query = "SELECT * FROM photorecord WHERE keywords LIKE '%" . $keyList[$q] . "%'"; } else if ($q > 0 && $q < count($keyList)) { $query .= " OR keywords LIKE '%" . $keyList[$q] . "%'"; } } } $query .= " ORDER BY logNum DESC"; //save query to session $_SESSION['query'] = $query; } else if ($searchType == "nav") { //$query = $_SESSION['query']; $query = $_SESSION['query']; $keysToSearch = $_SESSION['keysToSearch']; } echo $query; //get results $result = doQuery($query); $itemCount = mysql_num_rows($result); //show results $i=0; if ($itemCount > 0) { while ($itemCount > $i) { $i++; $logNum = mysql_result($result,$i-1,'logNum'); $logNumArray[] = $logNum; } } //figure out all the page stuff $pageCount = floor(round((count($logNumArray) / 18) + .5)); $page = $_GET['page']; if ($page == "") { $page = 1; } $minPageItem = ($page * 18) - 17; $maxPageItem = $minPageItem + 17; ?> <div id="searchdata" style="position: absolute; top: 50px; left: 100px; z-index: 0">Your search for "<?php echo $keysToSearch; ?>" produced <?php echo $itemCount; ?> result(s)<p>Showing items <?php echo $minPageItem; ?> thru <?php if ($maxPageItem > count($logNumArray)) { echo count($logNumArray); } else { echo $maxPageItem; } ?>. <br> <?php if ($page == 1) { $page = 2; echo "<a href='results.php?search=nav&page=" . $page . "'>[NEXT]</a>"; } else if ($page < $pageCount) { $pageUp = $page + 1; $pageDown = $page - 1; echo "<a href='results.php?search=nav&page=" . $pageUp . "'>[NEXT]</a> <a href='results.php?search=nav&page=" . $pageDown . "'>[PREVIOUS]</a>"; } else if ($page == $pageCount) { $page = $pageCount - 1; echo "<a href='results.php?search=nav&page=" . $page . "'>[PREVIOUS]</a>"; } ?> </div> Quote Link to comment Share on other sites More sharing options...
RIRedinPA Posted December 13, 2007 Author Share Posted December 13, 2007 OK, it worked once but then it failed. Grrrr. Quote Link to comment Share on other sites More sharing options...
RIRedinPA Posted December 13, 2007 Author Share Posted December 13, 2007 i fixed this, the session was working fine, the problem was in another part of my code. Quote Link to comment 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.