Page 1 of 1

PHP search script-I need help

Posted: Wed Aug 29, 2007 10:06 am
by tc
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi everyone,

Im very new to PHP and im trying to write a php search script that allows me to query my database for jobs and to display the results.  Ive designed it so that i can enter keywords eg. administrator, pilot etc and the results should returns jobs with those titles that fall into the job type(fulltime, part time etc) and region selected.  The problem is whenever i try doing a searh for jobs that i know are definitely stored in the database a blank screen appears, however if i try searching for a job that is not there it simply displays 'no results to display'.  I'm sure i've made a major mistake somewhere but i just don't know what it is.  Please help if you can.  Thanks....the code is below

Form code

[syntax="html"]<h3><font size="4">Search for Jobs</font></h3>
      <form name="form2" id="form2" method="get" action="jobsearch.php">
        <p>Position 
          <input type="text" name="position" />
        </p>
        <p>Type </p>
        <p>F/T 
          <input type="radio" name="jobtype" value="Full Time" />
          P/T 
          <input type="radio" name="jobtype" value="Part Time" />
        </p>
        <p>Temp
          <input type="radio" name="jobtype" value="Temporary" />
          Perm
          <input type="radio" name="jobtype" value="Permanent" />
        </p>
        <p>Region 
          <select name="region">
            <option>All regions</option>
            <option>Anglia</option>
            <option>East Anglia</option>
            <option>London</option>
            <option>London-North</option>
            <option>London-South</option>
            <option>Louth-East</option>
            <option>London-West</option>
            <option>Northern Ireland</option>
            <option>Scotland</option>
            <option>South East</option>
            <option>South West</option>
            <option>Wales</option>
          </select>
        </p>
        <p align="center"> 
          <input type="submit" name="Submit2" value="Search" />
        </p>
      </form>
PHP jobsearch code[/syntax]

Code: Select all

<?php
  
	$host="";
	$user="";
	$password="";

	
	$position=$_GET['position'];
	$type=$_GET['jobtype'];
	$region=$_GET['region'];
	
	$connect=mysql_connect($host, $user, $password) or
	die ("Sorry the connection to the database has failed, please check the server connection.");
	
	
	mysql_select_db("database", $connect);
	
	

   $sql = "SELECT * FROM job WHERE position LIKE '$position' AND type ='$type' AND region ='$region' ORDER BY JobRefNo DESC";  
   $query = mysql_query($sql) or die(mysql_error());  
    $row_sql = mysql_fetch_assoc($query);  
    $total = mysql_num_rows($query);  
      
    if($total>0) {  
        while ($row_sql = mysql_fetch_assoc($query)) {  
	   
       echo ''.$row_sql['position'].'<br />'.$row_sql['type'].'<br />'.$row_sql['region'].'';  
        }  
   } else  
       {  
      echo "No results to display";  
  } 
	mysql_close();
	
	
  ?>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Wed Aug 29, 2007 4:17 pm
by califdon
Most likely you're failing to see the first (and in some searches, the only) record that matches your query, because of this code:

Code: Select all

$query = mysql_query($sql) or die(mysql_error()); 
    $row_sql = mysql_fetch_assoc($query); 
    $total = mysql_num_rows($query); 
     
    if($total>0) { 
        while ($row_sql = mysql_fetch_assoc($query)) { 
          
       echo ''.$row_sql['position'].'<br />'.$row_sql['type'].'<br />'.$row_sql['region'].''; 
        } 
   } else 
       { 
      echo "No results to display"; 
  }
Every time you call mysql_fetch_assoc(), it moves to the next row of the query result. When you first assigned a row to $row_sql, you got the first row. But later, in the while loop, you assign the second row to $row_sql, so you've thrown away the values of the first row. Just remove the line above the $total =.

Thanks

Posted: Thu Aug 30, 2007 9:07 am
by tc
Thanks very much for your advice....it worked..:-)