homepage Welcome to WebmasterWorld Guest from 54.234.143.43
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / Databases
Forum Library, Charter, Moderators: physics

Databases Forum

    
MySQL: select single entry from multiple entries in fields
Jim123




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

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?

 

dreamcatcher




msg:4183101
 12:49 pm on Aug 6, 2010 (gmt 0)

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

dc

Jim123




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

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

dreamcatcher




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

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

dc

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Databases
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved