Page 1 of 1

displaying search results

Posted: Wed Dec 07, 2011 4:31 pm
by df75douglas
Hello , I am having an issue getting this php script to display all my data.

when you type in a city and submit it , it is suppose to give you all the fields
in the address table that have a city matching your search. But it just gives
the city, any one got ideas?
here is my code

Code: Select all

<html><title>hw5 search</title><body>

<form name="form" action="search.php" method="get">
  <input type="text" name="q" />
  <input type="submit" name="Submit" value="City" />
</form>

<?php

  // Get the search variable from URL

  $var = @$_GET['q'] ;
  $trimmed = trim($var); //trim whitespace from the stored variable, got this from a forum.

// 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  database 
mysql_connect("localhost","dfuller3","dfuller37318"); 

//specify database 
mysql_select_db("dfuller3") or die("Unable to select database"); 

// Build SQL Query  
$query = "select * from address where city like \"%$trimmed%\"  
  order by city"; 

 $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:<br>";
$count = 1 + $s ;

// now you can display the results returned
  while ($row= mysql_fetch_array($result)) {
  $title = $row["city;street"];

  echo "$count.)&nbsp;$title<br>" ;
 $count++ ;
  }



?>







</body>
</html>
thanks in advance

Re: displaying search results

Posted: Wed Dec 07, 2011 6:29 pm
by mikosiko
some problems in your code (among others):
- No reasons to execute the same (almost) query twice... inefficient... just move your "limit" to the first query and adjust the rest of the code accordingly.
- No code to control eventual errors in your query.
- According to this line $title = $row["city;street"]; you have a field in your table called "city;street"... I don't think so

Re: displaying search results

Posted: Thu Dec 08, 2011 3:35 pm
by social_experiment

Code: Select all

<?php
$title = $row["city;street"];
// should be
$title = $row['city'];
?>
To display fields you select, you need to use the appropriate column name from the table. If you have 'address' as a column name, $row['address'] would contain the information from the address field, and so forth for all the columns.

Adding to the pointers given by mikosiko; if you aren't going to use all the fields in your table, don't use SELECT * FROM table. Rather select only what you use. Also look at using mysql_real_escape_string() when passing input received from users into your database