Welcome to WebmasterWorld Guest from 184.73.18.109

Forum Moderators: open

MySQL: select single entry from multiple entries in fields

   
7:24 am on Aug 6, 2010 (gmt 0)

5+ Year Member



I have a database with the table houses with field names: 'doors', 'windows' and 'roofs'.

The data (fields) under 'windows' contains multiple entries divided by a comma, like this
round,blue
round,red
round,brown
square,blue
square,red
square,brown

People can fill in a keyword to search for with this form

<form action="insert.php" method="post">
Search windows -> Keyword: <input type="text" name="keyword1"><br>
<input type="Submit">
</form>


And insert.php is this

<?php
$username="root";
$password="";
$database="houssss";

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM houses WHERE windows='keyword1'";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();
?>
<table border="0" cellspacing="2" cellpadding="2">
<tr>
<th><font face="Arial, Helvetica, sans-serif">Value1</font></th>
<th><font face="Arial, Helvetica, sans-serif">Value2</font></th>
<th><font face="Arial, Helvetica, sans-serif">Value3</font></th>
<th><font face="Arial, Helvetica, sans-serif">Value4</font></th>
<th><font face="Arial, Helvetica, sans-serif">Value5</font></th>
</tr>

<?php
$i=0;
while ($i < $num) {

$f1=mysql_result($result,$i,"Shrt_Desc");

?>

<tr>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f1; ?></font></td>

</tr>

<?php
$i++;
}
?>


You guess it already, it's not working. I am struggling with this part
$query="SELECT * FROM houses WHERE windows='keyword1'";


How can I select those entries that matches the keyword?
12:49 pm on Aug 6, 2010 (gmt 0)

WebmasterWorld Senior Member dreamcatcher is a WebmasterWorld Top Contributor of All Time 10+ Year Member



$query="SELECT * FROM houses WHERE windows LIKE '%".mysql_real_escape_string($_POST['keyword1'])."%'";

dc
12:47 am on Aug 7, 2010 (gmt 0)

5+ Year Member



Thanks DC, from here I can continue for a while. I got the idea now.

I'll come back when I am stuck again.

Jim
6:05 am on Aug 12, 2010 (gmt 0)

WebmasterWorld Senior Member dreamcatcher is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Yeah, no worries Jim. We are here to help.

dc
 

Featured Threads

Hot Threads This Week

Hot Threads This Month