PHP search using PostgreSQL

A simple search form to search  the db and display the contents via PHP and PostgreSQL

Name the below as index.html

 

Search



Seach for: in



Name the below as search.php

 

 

$searching = $_POST[‘searching’];
$find = $_POST[‘find’];
$field = $_POST[‘field’];

//This is only displayed if they have submitted the form

if ($searching ==”yes”)
{
echo “

Results

”;

//If they did not enter a search term we give them an error
if ($find == “”)
{
echo “

You forgot to enter a search term”;
exit;
}

// We preform a bit of filtering
//$find = strtoupper($find);
$find = strip_tags($find);
$find = trim ($find);

// attempt a connection
// $dbh = pg_connect(“host=hostname port=5432 dbname=dbname  user= username password=password”);
//if (!$dbh) {
//  die(“Error in connection: ” . pg_last_error());
// }
include_once(“connection/connection.php”);

// execute query
$sql = “SELECT * from table where $field = ‘$find’ limit 30 “;

$result = pg_query($dbh, $sql);
if (!$result) {
die(“Error in SQL query: ” . pg_last_error());
}

echo “






”;

while ($row = pg_fetch_array($result)) {

echo “

”;
echo “”;
echo ” ”;
echo “”;
echo “”;

}
echo “

FirstnameLastnameEmail address
” . $row[0] . “” . $row[1] . “ ” . $row[2] . “
”;

$rows = pg_num_rows($result);

if ($rows == 0)
{
echo “Sorry, but we can not find an entry to match your query

”;
}

//And we remind them what they searched for
echo “Searched For: ” .$find;

echo “Number of rows : ” .$rows;

// free memory
pg_free_result($result);

pg_close($dbh);
}
?>

 

Follow me

Leo G

Is a Linux Hobbyist and Enthusiast. He Strongly believes in OpenSource Software and would like you to view and download his software at https://github.com/Leo-g
Follow me