Page 1 of 1
Search function
Posted: Sat Jan 07, 2006 11:48 am
by thcc2
hi all,
I'm a newbie to php and i'm currently doing a search function in PHP and PostgresSQL database and it gives some errors, so that i hope that someone can give me a sample code that can let me to have a better understanding of it.
Thks in advance
Posted: Sat Jan 07, 2006 12:39 pm
by Chris Corbyn
If you post what you've done so far we could have a look and try to spot the problems, or offer advice on better ways to do things

Posted: Sun Jan 08, 2006 8:30 pm
by thcc2
d11wtq wrote:If you post what you've done so far we could have a look and try to spot the problems, or offer advice on better ways to do things

This is my code which display
Database Search returned 0 items containing ""." once the page is loaded.
Code: Select all
<html>
<head>
<title> Database Search</title>
</head>
<body>
<p align="center">Database Search</p>
<?php
//added 05/01
require("lib.php");
if( !checkSession() ){
$msg = "Sorry, either you have not login, " .
"or session has expired.";
include("error.php");
exit;
}
$dbname = $_SESSION['dbname'];
$dbuser = $_SESSION['dbuser'];
$dbpass = $_SESSION['dbpass'];
$constr = "host=localhost port=5432 dbname=$dbname " .
"user=$dbuser password=$dbpass";
$dbconn = pg_connect($constr);
$msg = "";
if( !($dbconn) ){
echo("db connection failed");
$msg = "Database connection failed: " .
$php_errormsg;
include("error.php");
exit;
}
//end
$max = 0;
//$sqlcmd = "select display_name, mobile_numb, level, class from address_book " .
//"where display_name like '%display_name%'";
$sqlcmd = "select display_name, mobile_numb, level, class from address_book " .
"where display_name like 'display_name%'";
//$bmax = mysql_query("SELECT * from table WHERE columntobesearched like '%$search%'");
$result = pg_query($dbconn, $sqlcmd);
//echo($result);
while ($arr = pg_fetch_array($result))
{
$max++;
}
//echo($max);
//$count = $arr[0];
?>
<table border="0" cellspacing="0" width="100%">
<tr>
<td height="12" align="left" background="bg2.jpg" width="50%">
<?php
echo "<form action="seach.php" method="get"><input type="text" name="seach" value="$search"><input type="submit" value="Search">";
//echo "</td><td height="12" align="right" bgcolor="red" width="50%">";
//echo "By: Terence";
echo "<td height="12" align="right" background="bg2.jpg" width="100%" colspan="2">";
echo "<center>Database Search returned $max items containing "<i>$search</i>".</center>";
echo "</td></tr></table></form>";
$maxresult = 10;
//add
$sql_text = "select display_name, mobile_numb, level, class from address_book " .
"where display_name like '%display_name%'";
//$sql_text = ("SELECT * from table WHERE columntobesearched like '%$search%'");
if (!$page)
{
$page = 1;
}
$backpage = $page - 1;
$nextpage = $page + 1;
//modified at 06/01
$query = pg_query($dbconn, $sql_text);
//$query = mysql_query($sql_text);
$start = ($maxresult * $page) - $maxresult;
$num_rows = pg_num_rows($query);
//$num_rows = mysql_num_rows($query);
if ($num_rows <= $maxresult)
{
$num_pages = 1;
}
else if (($num_rows % $maxresult) == 0)
{
$num_pages = ($num_rows / $maxresult);
}
else
{
$num_pages = ($num_rows / $maxresult) + 1;
}
$num_pages = (int) $num_pages;
if (($page > $num_pages) || ($page < 0))
{
error("You have specified an invalid page number");
}
//$sql_text = $sql_text . " LIMIT $start, $maxresult";
//add
$sql_text = $sql_text . " LIMIT $start OFFSET $maxresult";
//$sql_text = $sql_text . " LIMIT $start " OFFSET "$maxresult";
$query = pg_query($dbconn, $sql_text);
if ($max>$maxresult)
{
echo "<center>- ";
if ($backpage)
{
echo "<a href="seach.php?search=$search&page=$backpage">Prev</a>";
echo("CC");
}
else
{
echo "Prev";
}
for ($i = 1; $i <= $num_pages; $i++)
{
if ($i != $page)
{
echo " <a href="seach.php?seach=$search&page=$i">$i</a> ";
echo("aa");
}
else
{
echo " $i ";
}
}
if ($page != $num_pages)
{
echo "<a href="search.php?seach=$search&page=$nextpage">Next</a> -";
echo("bb");
}
else
{
echo "Next -";
}
echo "</center>";
}
?>
<table border="0" cellspacing="0" width="100%">
<?php
$a = $start + 1;
//here
//while ($result = mysql_fetch_array($query)) {
//$result = pg_query($dbconn, $sqlcmd);
//while ($arr = pg_fetch_array($result))
while ($arr = pg_fetch_array($result))
{
?>
<tr>
<td height="12" align="left" background="bg2.jpg" width="70%">
<?php
printf("$a. <a href="%s">", $result['linkcolumn']);
printf("%s </a></td><td align="right" background="bg2.jpg">", $result['namecolumn']);
printf("%s ", $result['authorcolumn']);
printf("</a>");
$a++;
?>
</td>
</tr>
<tr>
<td colspan="2">
<?php printf("%s <br>", $result['descriptioncolumn']); ?>
</td>
</tr>
<?php
}
echo "</table>";
if ($max>$maxresult)
{
echo "<br><br><br><center>- ";
if ($backpage)
{
echo "<a href="seach.php?seach=$search&page=$backpage">Prev</a>";
}
else
{
echo "Prev";
}
for ($i = 1; $i <= $num_pages; $i++)
{
if ($i != $page)
{
echo " <a href="seach.php?search=$search&page=$i">$i</a> ";
}
else
{
echo " $i ";
}
}
if ($page != $num_pages)
{
echo "<a href="seach.php?search=$search&page=$nextpage">Next</a> -";
}
else
{
echo "Next -";
}
echo "</center>";
}
?>
</body>
</html>
I hope that someone can help me on this.
Thks in advance[/b]
Posted: Sun Jan 08, 2006 8:39 pm
by timvw
thcc2 wrote:
Code: Select all
$sqlcmd = "select display_name, mobile_numb, level, class from address_book " .
"where display_name like 'display_name%'";
Are there many people that have a display_name as display name?

)
You probably want this to be like '{$display_name}%' instead. Btw, where would $display_name come from? And where is the validation for it?
Posted: Sun Jan 08, 2006 10:01 pm
by thcc2
timvw wrote:thcc2 wrote:
Code: Select all
$sqlcmd = "select display_name, mobile_numb, level, class from address_book " .
"where display_name like 'display_name%'";
Are there many people that have a display_name as display name?

)
You probably want this to be like '{$display_name}%' instead. Btw, where would $display_name come from? And where is the validation for it?
Thks, you are right, it display
Database Search returned 6 items containing "".?
once the page is loaded and the search button is clicked, i believe that is some logical error there.