madmatt2006 Posted November 16, 2010 Share Posted November 16, 2010 Hello Guys I'm getting this error "Parse error: syntax error, unexpected T_VARIABLE in /var/www/testing/index.php on line 22" this is what's on line 22 "$limit=10; " from the code below, I'm new at PHP so idky it's happening any advice would be great! <html> <head> <title>designplace.org search script</title> <meta name="author" content="Steve R, http://www.designplace.org/"> </head> <!-- © http://www.designplace.org/ --> <body> <form name="form" action="search.php" method="get"> <input type="text" name="q" /> <input type="submit" name="Submit" value="Search" /> </form> <?php // Get the search variable from URL $var = @$_GET['q'] ; $trimmed = trim($var) //trim whitespace from the stored variable // rows to return $limit=10; // check for an empty string and display a message. if ($trimmed == "") { echo "<p>Please enter a search...</p>"; exit; } // check for a search parameter if (!isset($var)) { echo "<p>We dont seem to have a search parameter!</p>"; exit; } //connect to your database ** EDIT REQUIRED HERE ** mysql_connect("localhost","root","Kikass32"); //(host, username, password) //specify database ** EDIT REQUIRED HERE ** mysql_select_db("pchelp") or die("Unable to select database"); //select which database we're using // Build SQL Query $query = "select * from phpbb_posts where 1st_field like \"%$trimmed%\" order by 1st_field"; // EDIT HERE and specify your table and field names for the SQL query $numresults=mysql_query($query); $numrows=mysql_num_rows($numresults); // If we have no results, offer a google search as an alternative if ($numrows == 0) { echo "<h4>Results</h4>"; echo "<p>Sorry, your search: "" . $trimmed . "" returned zero results</p>"; // google echo "<p><a href=\"http://www.google.com/search?q=" . $trimmed . "\" target=\"_blank\" title=\"Look up " . $trimmed . " on Google\">Click here</a> to try the search on google</p>"; } // next determine if s has been passed to script, if not use 0 if (empty($s)) { $s=0; } // get results $query .= " limit $s,$limit"; $result = mysql_query($query) or die("Couldn't execute query"); // display what the person searched for echo "<p>You searched for: "" . $var . ""</p>"; // begin to show results set echo "Results"; $count = 1 + $s ; // now you can display the results returned while ($row= mysql_fetch_array($result)) { $title = $row["1st_field"]; echo "$count.) $title" ; $count++ ; } $currPage = (($s/$limit) + 1); //break before paging echo "<br />"; // next we need to do the links to other results if ($s>=1) { // bypass PREV link if s is 0 $prevs=($s-$limit); print " <a href=\"$PHP_SELF?s=$prevs&q=$var\"><< Prev 10</a>  "; } // calculate number of pages needing links $pages=intval($numrows/$limit); // $pages now contains int of pages needed unless there is a remainder from division if ($numrows%$limit) { // has remainder so add one page $pages++; } // check to see if last page if (!((($s+$limit)/$limit)==$pages) && $pages!=1) { // not last page so give NEXT link $news=$s+$limit; echo " <a href=\"$PHP_SELF?s=$news&q=$var\">Next 10 >></a>"; } $a = $s + ($limit) ; if ($a > $numrows) { $a = $numrows ; } $b = $s + 1 ; echo "<p>Showing results $b to $a of $numrows</p>"; ?> <!-- © http://www.designplace.org/ --> </body> </html> Quote Link to comment Share on other sites More sharing options...
btherl Posted November 16, 2010 Share Posted November 16, 2010 The error is being reported where it is found, but the real error is on the previous line (with code on it). There is a ";" missing here: $trimmed = trim($var) //trim whitespace from the stored variable Quote Link to comment Share on other sites More sharing options...
madmatt2006 Posted November 16, 2010 Author Share Posted November 16, 2010 Thanks for the quick reply I changed that line of code to this $trimmed = trim($var) //trim whitespace from the stored variable; But I still get this error Parse error: syntax error, unexpected T_VARIABLE in /var/www/testing/index.php on line 22 got any more suggestions to try, thanks for taking the time to help me out I appreciate it. Quote Link to comment Share on other sites More sharing options...
madmatt2006 Posted November 16, 2010 Author Share Posted November 16, 2010 stupid me I had a blond moment $trimmed = trim($var); //trim whitespace from the stored variable it works now thanks a bunch! Quote Link to comment Share on other sites More sharing options...
madmatt2006 Posted November 16, 2010 Author Share Posted November 16, 2010 I spoke to soon the page seems to load ok see here http://mypcmates.com/testing/index.php so 1 thing is fixed, thank you for that. But if I got to do a search it goes to this page http://mypcmates.com/testing/search.php?q=windows&Submit=Search with no results, this is the guide I'm trying to follow here http://www.designplace.org/scripts.php?page=1&c_id=25 found it via Google. Quote Link to comment Share on other sites More sharing options...
btherl Posted November 16, 2010 Share Posted November 16, 2010 Try changing $numresults=mysql_query($query); to $numresults=mysql_query($query) or die("Error in $query\n".mysql_error()); Quote Link to comment Share on other sites More sharing options...
madmatt2006 Posted November 16, 2010 Author Share Posted November 16, 2010 tried that still the same it goes to this address http://mypcmates.com/testing/search.php?q=windows&Submit=Search and the page displays Not Found The requested URL /testing/search.php was not found on this server. Apache/2.2.12 (Ubuntu) Server at mypcmates.com Port 80 I'm very new at php, but there is no /testing/search.php file but I guess that is that this code is for <form name="form" action="search.php" method="get"> <input type="text" name="q" /> <input type="submit" name="Submit" value="Search" /> </form> Quote Link to comment Share on other sites More sharing options...
madmatt2006 Posted November 16, 2010 Author Share Posted November 16, 2010 also if we can't work this out has anyone got some examples or links to a good guide on how to build a web page that searches a mysql database. Quote Link to comment Share on other sites More sharing options...
btherl Posted November 16, 2010 Share Posted November 16, 2010 Aha, that error message is very useful. It is probably looking for a php script called search.php (unless the web server is configured to send that request elsewhere). Try ONE of these two options: 1. Rename index.php to search.php 2. Or, replace search.php with index.php in the form action Quote Link to comment Share on other sites More sharing options...
madmatt2006 Posted November 16, 2010 Author Share Posted November 16, 2010 I renamed index.php to search.php and it works thanks so much! I was just wondering also with this code would it be possible to make it search more of the database? I was looking at the code below it searches the post_text field of the database, could it also search the post_id field of the database? $query = "select * from phpbb_posts where post_text like \"%$trimmed%\" order by post_text"; // EDIT HERE and specify your table and field names for the SQL query Your help has been great thanks! Quote Link to comment Share on other sites More sharing options...
btherl Posted November 17, 2010 Share Posted November 17, 2010 If you want to search post_id for the same string then you can do this: $query = "select * from phpbb_posts " . " where post_text like \"%$trimmed%\" " . " or post_id like \"%$trimmed%\" " . " order by post_text"; // EDIT HERE and specify your table and field names for the SQL query Or if post_id is a number and you want to search for the exact number only: $query = "select * from phpbb_posts " . " where post_text like \"%$trimmed%\" " . ((int)$trimmed > 0 ? (" or post_id = \"" . (int)$trimmed . "\" ") . "") . " order by post_text"; // EDIT HERE and specify your table and field names for the SQL query I've added something a bit complicated in the second version because if you are checking against an integer column, you need to make sure the value you're checking is also an integer. So that code only checks against post_id if $trimmed is an integer. Quote Link to comment Share on other sites More sharing options...
madmatt2006 Posted November 17, 2010 Author Share Posted November 17, 2010 I will explain why I'm looking at this code and trying to learn it so I can get the results I'm after, the table will be called "partslist" it will have these fields below, I want it to search NAME and DESCRIPTION fields for the search term, and when displaying the results it has all the information in NAME DESCRIPTION MANUFACTURER SALEPRICE PRICE BUYURL IMAGEURL ID NAME DESCRIPTION MANUFACTURER SALEPRICE PRICE BUYURL IMAGEURL would that be possible to do this by modifying this code? Quote Link to comment Share on other sites More sharing options...
btherl Posted November 17, 2010 Share Posted November 17, 2010 Yes that sounds quite possible. In that case you don't need to check if $trimmed is an integer. 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.