...

View Full Version : Searching mysql with php help!



angst
09-09-2005, 04:01 PM
Hello,
I'm trying to search mysql with php,
and have one or two problems,

my code:



<?php
include 'admin/conn.php';
$link = mysql_connect($SqlHost, $SqlUser, $SqlPass) or die('Login details incorrect');
$db_selected = mysql_select_db($SqlDb, $link) or die('Database name incorrect');

$search = $_REQUEST['search'];
$searchdrop = $_REQUEST['searchdrop'];

//Count Search Results
Function SearchCount($search,$searchdrop)
{

global $SqlHost;
global $SqlUser;
global $SqlPass;
global $SqlDb;


$link = mysql_connect($SqlHost, $SqlUser, $SqlPass) or die('Login details incorrect');
$db_selected = mysql_select_db($SqlDb, $link) or die('Database name incorrect');
$query = "SELECT * FROM listings WHERE keywords LIKE \"%$search%\" AND status=2 AND cid=$searchdrop";
$numguests = mysql_query($query) or die("Select Failed!");
$numguest = mysql_fetch_array($numguests);

echo "$numguest[0]";

}

echo "search results for \"<b>$search</b>\" returned ";
SearchCount("$search","$searchdrop");
echo " matches<br /><br />";


$result = mysql_query("SELECT * FROM listings WHERE keywords LIKE \"%$search%\" AND status=2 AND cid=$searchdrop");

while ($row3 = mysql_fetch_assoc($result))
{
echo "<table cellpadding=2 cellspacing=1 border=0 width=400 bgcolor='#E6E6E6'>";
echo "<tr><td bgcolor='#FF9E21' align=left><a href='$row3[link]' target='_blank'>$row3[Name]</td></tr>";
echo "<tr><td align=left>$row3[Description]</td></tr>";
echo "</table><br />";
}

?>



first problem,
my function:
Function SearchCount($search,$searchdrop)
to count the number of records found, gives me 9 for some reason, and only one record is being displayed using the same select query as you can see, or maybe I'm just not seeing something.

also if I just click on the search button on my search form, it returns everything in the mysql table i'm searching, why does it do this?

thanks in advance for your time!
-Ken

devinemke
09-09-2005, 05:09 PM
<?php
include('admin/conn.php');
mysql_connect($SqlHost, $SqlUser, $SqlPass) or exit(mysql_error());
mysql_select_db($SqlDb, $link) or exit(mysql_error());

$search = $_REQUEST['search'];
$searchdrop = $_REQUEST['searchdrop'];

$sql = "SELECT * FROM listings WHERE keywords LIKE '%$search%' AND status = '2' AND cid = '$searchdrop'";
$result = mysql_query($sql) or exit(mysql_error());
$num_rows = mysql_num_rows($result);

echo 'search results for <b>' . $search . '</b> returned ' . $num_rows . ' matches<br /><br />';

if ($num_rows)
{
echo '<table cellpadding="2" cellspacing="1" border="0" width="400" bgcolor="#E6E6E6">';
while ($row = mysql_fetch_assoc($result))
{
echo '
<tr><td bgcolor="#FF9E21" align="left"><a href="' . $row['link'] . '" target="_blank">' . $row['Name'] . '</td></tr>
<tr><td align="left">' . $row['Description'] . '</td></tr>
';

}
echo '</table>';
}
?>

angst
09-09-2005, 05:14 PM
ah kool!

thanks!
-Ken



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum