Forum Moderators: coopster

Message Too Old, No Replies

Parse error: syntax error, unexpected T VARIABLE

Parse error: syntax error, unexpected T_VARIABLE

         

bopravi

9:15 am on Apr 20, 2011 (gmt 0)

10+ Year Member



Hello ,

I am a newbie in php. Trying to make a file which searches my Mysql database for some name and then gives out result.

But I keep getting this error:
Parse error: syntax error, unexpected T_VARIABLE in /home2/aljoufsc/public_html/search.php on line 9

If I comment that line it moves to another line. I am including the script for your reference.
Please help
Thanks
Ravi

***************
<?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 **
mysql_connect("localhost","aljoufsc_aljou12","aljou12123"); //(host, username, password)

//specify database **
mysql_select_db("aljoufsc_aljou") or die("Unable to select database"); //select which database we're using

// Build SQL Query
$query = "select * from es_preadmission where pre_name like \"%$trimmed%\"
order by 1st_field"; // ** 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: &quot;" . $trimmed . "&quot; 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: &quot;" . $var . "&quot;</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.)&nbsp;$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 "&nbsp;<a href=\"$PHP_SELF?s=$prevs&q=$var\">&lt;&lt;
Prev 10</a>&nbsp&nbsp;";
}

// 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 "&nbsp;<a href=\"$PHP_SELF?s=$news&q=$var\">Next 10 &gt;&gt;</a>";
}

$a = $s + ($limit) ;
if ($a > $numrows) { $a = $numrows ; }
$b = $s + 1 ;
echo "<p>Showing results $b to $a of $numrows</p>";

?>
****************

Please help

Thanks
Ravi

Matthew1980

9:46 am on Apr 20, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there BopRavi,

Welcome to the forums!

$trimmed = trim($var);

Add the semi colon to the end of that statement, then it will remove the error, whether it brings up a fresh error further down the line I can't say..

The script is very insecure like this; I would read up on data sanitising & I would try to exemplify your data before posting as you don't want everyone knowing your passwords....

Cheers,
MRb

bopravi

10:23 am on Apr 20, 2011 (gmt 0)

10+ Year Member



Hi Matthew,

Thanks for that reply. The passwords and DB name are just from my localhost..no issues

The error went away and I got a new error:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home2/aljoufsc/public_html/search.php on line 36

Line 36 is : $numrows=mysql_num_rows($numresults);

Thanks for your help in advance

Ravi

Matthew1980

11:22 am on Apr 20, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$numresults=mysql_query($query) or die(mysql_error());

This will detail whats wrong with your sql statement, then you should see whats causing this to go wrong.

This is how this should be formatted too :)

$query = "SELECT * FROM `es_preadmission` WHERE `pre_name` like '%".$trimmed."%' ORDER BY `1st_field`";

Cheers,
MRb

bopravi

4:48 pm on Apr 20, 2011 (gmt 0)

10+ Year Member



Hi ,

The query is working , but it is not displaying any results. This is the output:
You searched for: "aaish"

Results1.)
Showing results 1 to 1 of 1

Sorry for bugging you.. any help :)

Thanks
Ravi

rocknbil

5:36 pm on Apr 20, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Definitely add an error trap.
edited: Thought you had quotes missing . . . missed the order by on next line.

bopravi

7:56 pm on Apr 20, 2011 (gmt 0)

10+ Year Member



ok thanks mate...all lost today, maybe tomorrow