Jump to content

PHP/MySQL - searching for string in table


trassalg

Recommended Posts

I'm stuck.  Below is the code for both of my documents as well as the 2 includes.  I've included the document titles followed by the code.  Hopefully there's a simple solution that I'm not seeing?

 

I want to be able to search keywords or search strings (partial words) in either the articleText or articleTitle field and have those full articles printed out in the result screen (including article date, etc.) but without receiving duplicate results (for example, the same keyword appears in the title and the article)

 

Thanks much in advance for any help!

 

- - - - -

 

buildArticleList.php

 

<html>

<head>

<title>Baso de datos de Rodolfo</title>

<link href="styles/styles.css" rel="stylesheet" type="text/css">

<?php

include("includes/menuBar.php");

include("includes/misc.inc");

echo "<h2>Search for articles</h2>";

?>

</head>

<body>

 

 

<form method="post" action="buildArticleList_Submit.php">

<table width=90% align=center>

<tr>

  <td>Search for:</td>

    <td><input type=text name='search' size=60 maxlength=255></td>

  </tr>

  <td></td>

<td><input type=submit></td>

  </tr>

</table>

</form>

</body>

</html>


 

buildArticleList_Submit.php

 

<?

include("includes/misc.inc");

include("includes/menuBar.php");

 

$connection = mysql_connect($host,$user,$password);

if (!$connection) {die('Could not connect : ' . mysql_error());}

else {echo ("<h1>SUCCESSFUL CONNECTION</h1>");}

 

$db = mysql_select_db($database,$connection);

if (!$db) {die ('Could not connect to database : ' . mysql_error());}

else {echo ('You connected to the database!<br>');}

 

if ($search) // perform search only if a string was entered.

 

$srch="%".$search."%";

$query = "SELECT * FROM articleTable WHERE articleTitle LIKE '$srch' || articleText LIKE '$srch'";

 

$result = mysql_db_query($database, $query);

 

if ($result)

{

echo "Here are the results:<br><br>";

 

while ($row = mysql_fetch_array($result)) { // Begin while

$artID = $row["articleIDNumber"];

$logo = $row["logoURL"];

$dateOf = $row["dateOfArticle"];

$categ = $row["categories"];

$title = $row["articleTitle"];

$text = $row["articleText"];

 

echo "{$row['$artID']}<br>

{$row['$logo']}<br>

{$row['$dateOf']}<br>

{$row['$categ']}<br>

{$row['$title']}<br>

{$row['$text']}

</tr>

<tr>

<td>$artID</td>

<td>$logo</td>

<td>$dateOf</td>

<td>$categ</td>

<td>$title</td>

<td>$text</td>

</tr>

</table>";

} // end while

}

else {

echo "<br><br><br><div align='center'>Error</div>";

}

?>


 

misc.inc

 

<?php

  $host="localhost:8889";

  $user="root";

  $password="root";

  $database="rodolfo";

?>


 

menuBar.php

 

<html>

<head>

<title></title>

</head>

 

<body>

<table width="620" border="0" align="center">

  <tr>

  <td width="20%" valign="top" bgcolor="#CCCCCC"><a href="addArticle.php">Add An Article</a></td>

  <td width="20%" valign="top" bgcolor="#999999"><a href="walkThru.php">Browse/Update Articles</a></td>

  <td width="20%" valign="top" bgcolor="#CCCCCC"><a href="buildArticleList.php">Build An Article List For Clients</a></td>

  <td width="20%" valign="top" bgcolor="#999999"><a href="advertiserMaintenance.php">Advertister/Logo maintenance</a></td>

  <td width="20%" valign="top" bgcolor="#CCCCCC">Backup Database</td>

  </tr>

</table>

<br>

<br>

</body>

</html>


Link to comment
https://forums.phpfreaks.com/topic/61327-phpmysql-searching-for-string-in-table/
Share on other sites

Unfortunately, no change.  I've updated a couple things (cleaning up a bit).  Here's the new buildArticleList.php file, including your suggested change.

 


 

<html>

<head>

<link href="styles/styles.css" rel="stylesheet" type="text/css">

</head>

<body>

<?

include("includes/misc.inc");

include("includes/menuBar.php");

 

$connection = mysql_connect($host,$user,$password);

if (!$connection) {die('Could not connect : ' . mysql_error());}

else {echo ("SUCCESSFUL CONNECTION<br>");}

 

$db = mysql_select_db($database,$connection);

if (!$db) {die ('Could not connect to database : ' . mysql_error());}

else {echo ('You connected to the database!<br><br>');}

 

if ($search); // perform search only if a string was entered.

$srch="%".$search."%";

$query = "SELECT * FROM articleTable WHERE articleTitle LIKE '$srch' or articleText LIKE '$srch'";

 

$result = mysql_db_query($database, $query);

 

if ($result)

{

echo "Here are the results:<br><br>";

 

while ($row = mysql_fetch_array($result)) { // Begin while

$artID = $row["articleIDNumber"];

$logo = $row["logoURL"];

$dateOf = $row["dateOfArticle"];

$categ = $row["categories"];

$title = $row["articleTitle"];

$text = $row["articleText"];

 

echo "<tr>

<td>

{$row['$artID']}<br>

{$row['$logo']}<br>

{$row['$dateOf']}<br>

{$row['$categ']}<br>

{$row['$title']}<br>

{$row['$text']}

</td>

</tr>

</table>";

} // end while

}

else {

echo "<br><br><br><div align='center'>Error</div>";

}

?>

</body>

</html>

try this code..

 


<?
include("includes/misc.inc");
include("includes/menuBar.php");

$connection = mysql_connect($host,$user,$password);
   if (!$connection) {die('Could not connect : ' . mysql_error());}
   else {echo ("SUCCESSFUL CONNECTION");}

$db = mysql_select_db($database,$connection);
   if (!$db) {die ('Could not connect to database : ' . mysql_error());}
   else {echo ('You connected to the database!');}
   

if (!empty($search)){ // perform search only if a string was entered.
$srch = "%".$search."%";
$query = "SELECT * FROM articleTable WHERE articleTitle LIKE '$srch' OR articleText LIKE '$srch'";
$result = mysql_query($query);

if ($result){
echo "Here are the results:";
$table_content = "";
$table_content .= "<table>";
while ($row = mysql_fetch_array($result)) { // Begin while
	$table_content .= "<tr>
		<td>".$row['articleIDNumber']."</td>
		<td>".$row['logoURL']."</td>
		<td>".$row['dateOfArticle']."</td>
		<td>".$row['categories']."</td>
		<td>".$row['articleTitle']."</td>
		<td>".$row['articleText']."</td>
		</td>
	  	</tr>";
}
$table_content .= "</table>";
echo $table_content;
}
else {
echo "<div align='center'>Error</div>";
}
else {

echo 'search string is empty!!!!!!!! tengtengtengteng!!!';
}
?>

I think you should print query result to make sure whether the result is empty or not.

 

like this: print_r($result);

 

.. if no result there's something wrong with your query.

 

change this chunk of code

$srch = "%".$search."%";
$query = "SELECT * FROM articleTable WHERE articleTitle LIKE '$srch' OR articleText LIKE '$srch'";

 

to this..

 

$srch = "%".$search."%";
$query = "SELECT * FROM articleTable WHERE articleTitle LIKE %".$search."% OR articleText LIKE %".$search."%";

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.