[SOLVED] My search script doesn't work!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gojira999
Forum Newbie
Posts: 5
Joined: Fri Sep 03, 2004 7:42 am
Location: Monster Island

[SOLVED] My search script doesn't work!

Post by gojira999 »

feyd | Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


Hello all.

I am on day 3 of learning how to use mysql/php and have written the script below.

Unfortunately it doesn't work!

Where I enter attributes where a return is expected I get the message

Code: Select all

You have an error in your SQL syntax near '' at line 12
I have added an echo statement on the select line and have tested the select line in mysql where it works fine.

I have a feeling it is something obvious I am missing :oops: but would be grateful for any advice. The script is as follows:

Code: Select all

<html>
<head>
<title>PubSearch</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<form name="form1" method="get" action="pubsearch.php">
  <p><font color="#000000" face="Arial, Helvetica, sans-serif">First part of post 
    code (eg GL1): 
    <input name="pc1" type="text" size="5" maxlength="4">
    </font></p>
  <p><font color="#000000" face="Arial, Helvetica, sans-serif">Second part: 
    <input name="pc2" type="text" size="3" maxlength="1">
    </font></p>
  <p> <font color="#000000" face="Arial, Helvetica, sans-serif"> Type of Venue: 
    <select name="ven">
      <option>Pub</option>
      <option>Resturaunt</option>
      <option>Night Club</option>
      <option>Taxi</option>
    </select>
    </font></p>
  <p><font color="#000000" face="Arial, Helvetica, sans-serif">
    <input type="submit" name="Submit" value="Submit">
    </font></p>
</form>
<?php
$var = @$_GET['pc1'] ;
$a = addslashes(trim($var)); 

$var = @$_GET['pc2'] ;
$b = addslashes(trim($var)); 

$var = @$_GET['ven'] ;
$c = addslashes(trim($var));

mysql_connect("localhost","","");

mysql_select_db("pubs") or die("Unable to select database");

$query  = "SELECT
  DISTINCT location.postalcode,
  location.postal,
  venue.VenueType,
  venue.Name
FROM
  location LEFT JOIN venue
    ON location.RegionCode = venue.RegionCode
WHERE
  location.postalcode = '$a' AND
  location.postal = '$b' AND
  venue.VenueType = '$c'"; echo $query;

$numresults = mysql_query($query);
$numrows = mysql_num_rows($numresults);

if ($numrows == 0) {
    echo "<h4>Results</h4>";
    echo "<p>Sorry, your search: "" . $a. $b .$c. "" returned zero results</p>";
} else {
    if (empty($s)) {
        $s=0;
    }

    $query .= " limit $s,$limit";
   
$result = mysql_query($query) or die(mysql_error());

    echo "<p>You searched for: "" . $var . ""</p>";
    echo "Results";
    $count = 1 + $s ;

    while ($row= mysql_fetch_array($result)) {
        $title = $row["name"];
        echo "$count.)&nbsp;$title" ;
        $count++ ;
    }

    $currPage = (($s/$limit) + 1);
}
?>
  
  
  
</body>
</html>
Thanks for looking! :lol:


feyd | Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post by scorphus »

Where does $limit come from? It's not being even declared until the new query definition:

Code: Select all

<?php
//...
$query .= " limit $s,$limit";
//...
?>
That error message is rised by:

Code: Select all

<?php
//...
$result = mysql_query($query) or die(mysql_error());
//...
?>
It seems your SQL query is with an endind comma (,), something like this:

Code: Select all

SELECT
  DISTINCT location.postalcode,
  location.postal,
  venue.VenueType,
  venue.Name
FROM
  location LEFT JOIN venue
    ON location.RegionCode = venue.RegionCode
WHERE
  location.postalcode = 'xxx' AND
  location.postal = 'xxx' AND
  venue.VenueType = 'xxx' limit 0,
-- Scorphus
wasabi
Forum Newbie
Posts: 17
Joined: Sat Sep 04, 2004 9:38 am
Location: Adelaide

Search Script

Post by wasabi »

Hack at this script.

Search, print results and split results into units of 10.

Code: Select all

<?php
$searchresults = 5;
	
trim($searchterm);

if (!isset($start)) $start = 0;
	
$searchtype = addslashes($searchtype);
$searchterm = addslashes($searchterm);
  
if ($searchterm == "") 
    {
exit;
}

$SQL = "SELECT * FROM page WHERE ".$searchtype." LIKE '%".$searchterm."%'";

$result=mysql_query($SQL,$dB);
$totalnum = mysql_num_rows($result);

$SQL = "SELECT * FROM page WHERE ".$searchtype." LIKE '%".$searchterm."%' ORDER BY unitid ASC LIMIT " . $start . ",$searchresults";

$result=mysql_query($SQL,$dB);

$num = mysql_num_rows($result);
$cur = 1;
	
echo "<table border="0" width="100" cellpadding="5" align="center">";
echo "<tr><td nowrap valign="top"><font class="heading">You searched for " $searchterm ".</font></td><td nowrap valign="top"> <font class="searchresults">Total number of results found = $totalnum </p> </td></tr></table>";
echo "<table width="600" cellpadding="8" cellspacing="0">";

while ($num >= $cur) 
   {
$row = mysql_fetch_array($result);

$unitid = $row['unitid'];
$page_content = stripslashes($row['page_content']); 
$cont = htmlspecialchars(stripslashes($row['cont']));
$link = $row['link'];
$category = $row['category'];
$title = htmlspecialchars(stripslashes($row['title']));

$cont = str_replace ("$searchterm", "[redcaps] $searchterm [/redcaps]", $cont );
	
$cont = str_replace ("[redcaps]", "<font class="redcaps">", $cont);
$cont = str_replace ("[/redcaps]", "</font>", $cont);
	
echo "<tr><td valign="top">&nbsp;</td></tr><tr><td><font class="paragraph">$cont</font></td><tr><td><a href='index.php?display=3&searched=$unitid' class='SEA'>Click here to read more</a></td></tr>";
	
$cur++;
	
}

echo "</table><br>";

$currentsearch = "&searchterm=$searchterm" . "&searchtype=$searchtype";
echo "<table width="100" cellpadding="8" cellspacing="0"><tr>";

if ($start > 0) 
{
echo "<td nowrap><a href="$PHP_SELF?display=3&start=" . ($start - $searchresults) . $currentsearch . "">Previous</a></td>";  
}

for ($numcounter=0; $numcounter<$totalnum; $numcounter+=5) 
{
$poscounter++;
echo "<td nowrap><a href="$PHP_SELF?display=3&start=" . ($numcounter) . $currentsearch . "">[$poscounter]</a></td>";
}
  
if (($start + $searchresults) < $totalnum)
{
echo "<td nowrap><a href="$PHP_SELF?display=3&start=" . ($start + $searchresults) . $currentsearch . "">Next</a><br>\n</td>";
}
elseif ($start > $res)
{ 
$res = $totalnum - $start;
echo "<td nowrap><font class="searchresults">End of $totalnum results</p></td></tr></table>";
exit;
}
elseif ($totalnum > $start)
{ 
echo "<td nowrap><font class="searchresults">End of $totalnum results </p></td></tr></table>";
exit;
}
elseif ($totalnum == $searchresults)
{
echo "<td nowrap><font class="searchresults"> End of $totalnum results; Please try another query</p></td>";
}	
else
{
echo "<td nowrap><font class="searchresults">The query " $searchterm " returned no results;<br> Please try another query </p></td>";
}
echo " </tr></table><br>";
?>
Hope this helps you.
gojira999
Forum Newbie
Posts: 5
Joined: Fri Sep 03, 2004 7:42 am
Location: Monster Island

Post by gojira999 »

Thanks for the replies/assistance.

I have rebuilt the application from the ground up using a simpler design & low and behold it works!!!! :oops:

I have learnt a valuable "walk before running" lesson here......

Best wishes.

Gojira
Post Reply