Author Topic: searching a PostgreSQL database  (Read 1600 times)

0 Members and 1 Guest are viewing this topic.

Offline el_ninoTopic starter

  • Irregular
  • Posts: 28
    • View Profile
searching a PostgreSQL database
« on: December 29, 2009, 07:16:30 AM »
hey...

I have a search function which is used to search my PostgreSQL database. I have included the code below
Code: [Select]
<html>
<head>
<title>Search</title>
</head>

<div align="center">

<?php
include ('includes/header.html')
?>


<body>
<p>&nbsp </p>
<p>Enter a Author or Article title to begin searching</p>
<p>&nbsp </p>

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

<p></p>
<p>&nbsp </p>
</body>

<?php
//include ('includes/footer.html')
?>


</div>

</html>

and...
Code: [Select]
<html>

<head>
<title>Search</title>
</head>

<?php
include ('includes/header.html');
?>


<div align="center">

<body>

<p>&nbsp</p>


<?php
//connecting to the database
include('includes/db_connect.php')
?>


<?php

//$sql = "select * from jfs where author ilike '%$search_author%' order by issue";

//$sql = "select* from jfs where author like '%searchstring%' order by issue";

$sql "select * from jfs where author ilike '%$search_author%'" 

            
$result_set pg_Exec ($conn$sql);
            
$rows pg_NumRows($result_set);

            if ((!
$result_set) || ($rows 1)) {
                  echo 
"<H1>ERROR - no rows returned</H1><P>";
                  exit;  
//exit the script
                  
}

            for (
$j=0$j $rows$j++)
               {
               
$issue pg_result($result_set$j"issue");
               
$page pg_result($result_set$j"page");
               
$author pg_result($result_set$j"author");
               
$title pg_result($result_set$j"title");
               
$format pg_result($result_set$j"format");
               
$url pg_result($result_set$j"url");


?>

<table table border="3" width=60%>
<tr><th>Issue</th><th>Page</th><th>Author</th><th>Title</th><th>Format</th><th>URL</th></tr>
              <TR>
              <TD width=10%>
<?php echo "<a href=$url$issue </a>"?>
              </TD>
              <TD width=10%>
<?php echo "<a href=$url$page </a>"?>
              </TD>
              <TD width=15%>
<?php echo "<a href=$url$author </a>"?>
              </TD>
              <TD>
<?php echo "<a href=$url$title </a>"?>
              </TD>
              <TD width=10%>
<?php echo "<a href=$url$format </a>"?>
              </TD>
  <TD width=30%>
              <?php echo "<a href=$url$url </a>"?>
              </TD>
              </TR>
 
  </table>

<?php

               }
?>


 
<?php
               pg_FreeResult
($result_set);
               
pg_Close($conn);
?>

</TABLE>

</body>

</div>


<?php
//include ('includes\footer.html');
?>


</html>

this works in that i get a set of results when the user enters a search term and click search and do not get results when no term is entered but the problem is that no matter what term is entered into the search field the same results show up every time.

help?

el nino


Offline btherl

  • Guru
  • Fanatic
  • *
  • Posts: 3,791
  • Gender: Male
  • Matt is the best!
    • View Profile
Re: searching a PostgreSQL database
« Reply #1 on: December 29, 2009, 07:29:09 AM »
Your form input is called "q", so you should access that with either $_POST['q'] or $_REQUEST['q'] in the code.
Your php questions answered at Flingbits

Offline el_ninoTopic starter

  • Irregular
  • Posts: 28
    • View Profile
Re: searching a PostgreSQL database
« Reply #2 on: December 29, 2009, 08:02:10 AM »
cheers  :D