Søgning med PHP/MySQL
Jeg sidder med et nyhedssystem det køre fra en MySQL database. Jeg vil gerne kunne søge efter indehold i den table der hedder memoPostContents i sn_post. Har selv prøvet at kode det, men uden held pga. en fejl eller mangel et sted i koden.Her er hele koden til index.php:
----------------------------------------------------------------
<?php require_once('Connections/myconn.php'); ?>
<?php require_once('Connections/myconn.php'); ?>
<?php
mysql_select_db($database_myconn, $myconn);
$query_rsCat = "SELECT * FROM sn_category ORDER BY intCatSort ASC";
$rsCat = mysql_query($query_rsCat, $myconn) or die(mysql_error());
$row_rsCat = mysql_fetch_assoc($rsCat);
$totalRows_rsCat = mysql_num_rows($rsCat);
$maxRows_rsPosts = 8;
$pageNum_rsPosts = 0;
if (isset($_GET['pageNum_rsPosts'])) {
$pageNum_rsPosts = $_GET['pageNum_rsPosts'];
}
$startRow_rsPosts = $pageNum_rsPosts * $maxRows_rsPosts;
mysql_select_db($database_myconn, $myconn);
$query_rsPosts = "SELECT intPostID, txtPostTitle, intPostBy, memoPostContents, DATE_FORMAT(dtePostDate, '%d/%m/%Y, %H:%i') AS dtePostDate FROM sn_post ORDER BY dtePostDate DESC";
$query_limit_rsPosts = sprintf("%s LIMIT %d, %d", $query_rsPosts, $startRow_rsPosts, $maxRows_rsPosts);
$rsPosts = mysql_query($query_limit_rsPosts, $myconn) or die(mysql_error());
$row_rsPosts = mysql_fetch_assoc($rsPosts);
if (isset($_GET['totalRows_rsPosts'])) {
$totalRows_rsPosts = $_GET['totalRows_rsPosts'];
} else {
$all_rsPosts = mysql_query($query_rsPosts);
$totalRows_rsPosts = mysql_num_rows($all_rsPosts);
}
$totalPages_rsPosts = ceil($totalRows_rsPosts/$maxRows_rsPosts)-1;
$SearchResult_rsSearchResult = "%";
if (isset($_GET['SearchResult'])) {
$SearchResult_rsSearchResult = (get_magic_quotes_gpc()) ? $_GET['SearchResult'] : addslashes($_GET['SearchResult']);
}
mysql_select_db($database_myconn, $myconn);
$query_rsSearchResult = sprintf("SELECT * FROM sn_post WHERE memoPostContents LIKE '%%%s%%' ORDER BY dtePostDate DESC", $SearchResult_rsSearchResult);
$rsSearchResult = mysql_query($query_rsSearchResult, $myconn) or die(mysql_error());
$row_rsSearchResult = mysql_fetch_assoc($rsSearchResult);
$totalRows_rsSearchResult = mysql_num_rows($rsSearchResult);
?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Velkommen til Stargatefan.dk</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="/style.css" rel="stylesheet" type="text/css">
</head>
<body>
<table border="0" cellspacing="0" cellpadding="0" align="center" width="720">
<tr>
<th scope="row" class="info">http://stargatefan.dk copyright Christian Brædstrup - admin@stargatefan.dk</th>
</tr>
</table>
<table width="720" height="100%" border="0" align="center" cellpadding="0" cellspacing="0" id="Post_total">
<td>
<h1>Stargatefan.dk</h1>
<div class="top_navbar"><a href="../index.php">Home</a> | <a href="/arkiv/index.php">Arkiv</a> | <a href="/galleri/index.php">Galleri</a> | <a href="downloads/index.php">Downloads</a> | <a href="/links/index.php">links</a> </div>
</div>
<form action="index.php" method="get" name="frmSearch" id="frmSearch">
<input type="text" name="textfield">
<input type="submit" name="Submit" value="Søg">
</form>
</td>
<tr>
<td scope="row" valign="top" height="100%" id="Post_total" align="center">
<?php do { ?>
<?php if ($totalRows_rsPosts > 0) { // Show if recordset not empty ?>
<?php } // Show if recordset not empty ?>
<table border="0" cellspacing="0" cellpadding="0" id="post">
<tr>
<td width="700" class="headline" scope="row"><?php echo $row_rsPosts['dtePostDate']; ?> <?php echo $row_rsPosts['txtPostTitle']; ?></td>
</tr>
<tr>
<td id="table_text" scope="row" width="700">
<p class="postText"><?php echo $row_rsPosts['memoPostContents']; ?></p>
<p class="postBy">skrevet af: <?php echo $row_rsPosts['intPostBy']; ?> </p>
</td>
</tr>
</table>
<?php } while ($row_rsPosts = mysql_fetch_assoc($rsPosts)); ?>
</td>
</tr>
</table>
<?php if ($totalRows_rsPosts == 0) { // Show if recordset empty ?>
<table width="720" height="45" border="0" cellpadding="0" cellspacing="0">
<tr>
<td scope="row" id="Post_total">Der er desværre ingen nyheder i databasen.</td>
</tr>
</table>
<?php } // Show if recordset empty ?>
<p> </p>
</body>
</html>
<?php
mysql_free_result($rsCat);
mysql_free_result($rsPosts);
mysql_free_result($rsSearchResult);
?>