Hello!
I'm working with a MySQL db for the fist time. I have a table called jpegs_tbl that holds information relating to jpeg images which are stored in a directory on my webserver.
jpegs_tbl has a column called desription which holds a description of each image and a column called keywords which holds keywords relating to each image.
What I'm trying to do is build a search engine that will look for matching words in both columns. So if the user types in "tree" the search engine will look for images that have the word tree in either of the two columns.
But at the moment I can't find the correct SQL to do a search on just one column let alone two!
<?php
$colname_rs_images = "-1";
if (isset($_GET['keyword'])) {
$colname_rs_images = (get_magic_quotes_gpc()) ? $_GET['keyword'] : addslashes($_GET['keyword']);
}
mysql_select_db($database_rb_photo, $rb_photo);
$query_rs_images = sprintf("SELECT * FROM jpegs_tbl WHERE keywords LIKE '%s'", '%'.$colname_rs_images.'%');
$rs_images = mysql_query($query_rs_images, $rb_photo) or die(mysql_error());
$row_rs_images = mysql_fetch_assoc($rs_images);
$totalRows_rs_images = mysql_num_rows($rs_images);
?>
This code works if the image has just one keyword "tree" or multiple keywords including tree. However, if the image has multiple keywords like "lake mountain tree" and the user searches for "lake tree" the image will not be returned. It will only be found if the user types in the exact same string "lake mountain tree" or any single keyword like "lake".
The above code was writern by Dreamweaver then I added LIKE and the %'s to $colname_rs_images. There are 2 things I don't understand:
1. Why is "$colname_rs_images = "-1";" needed, I can't see that it does anything.
2. What does " '%s' " do?
Many thanks for your help! You all rock!!!