blackdog Posted June 13, 2008 Share Posted June 13, 2008 I am trying to create a search function that can return results that don't necessarily match the search exactly. I have created the following mySQL query using dreamweaver 8 which only returns results that match the search exactly. Can anyone help me with this? Here is the code that I/Dreamweaver have created (I am using mySQL v. 4.1.22): <?php $colname_rsPtInfo = "-1"; if (isset($_POST['Search'])) { $colname_rsPtInfo = $_POST['Search']; } $colname_rsPtInfo = "-1"; if (isset($_GET['Search'])) { $colname_rsPtInfo = (get_magic_quotes_gpc()) ? $_GET['Search'] : addslashes($_GET['Search']); } mysql_select_db($database_corys, $corys); $query_rsPtInfo = sprintf("SELECT * FROM PtInfo WHERE Last_Name = '%s' ORDER BY Last_Name ASC", $colname_rsPtInfo); $rsPtInfo = mysql_query($query_rsPtInfo, $corys) or die(mysql_error()); $row_rsPtInfo = mysql_fetch_assoc($rsPtInfo); $totalRows_rsPtInfo = mysql_num_rows($rsPtInfo); ?> Quote Link to comment https://forums.phpfreaks.com/topic/110002-using-dreamweaver-to-create-a-php-mysql-search-function/ Share on other sites More sharing options...
zenag Posted June 13, 2008 Share Posted June 13, 2008 SELECT * FROM PtInfo WHERE Last_Name LIKE '%s' Quote Link to comment https://forums.phpfreaks.com/topic/110002-using-dreamweaver-to-create-a-php-mysql-search-function/#findComment-564544 Share on other sites More sharing options...
blackdog Posted June 16, 2008 Author Share Posted June 16, 2008 I tried changing th = to LIKE and it still only returns exact matches. Any other ideas? Quote Link to comment https://forums.phpfreaks.com/topic/110002-using-dreamweaver-to-create-a-php-mysql-search-function/#findComment-566268 Share on other sites More sharing options...
fenway Posted June 16, 2008 Share Posted June 16, 2008 Give some examples. Quote Link to comment https://forums.phpfreaks.com/topic/110002-using-dreamweaver-to-create-a-php-mysql-search-function/#findComment-566446 Share on other sites More sharing options...
blackdog Posted June 16, 2008 Author Share Posted June 16, 2008 An example would be that in this database I have a patient with the last name of Frost. If I search for "Frost" in my search form field it brings up his record. What I am looking to do is be able to search for "Fr" and have it bring up any record which matches the "Fr" so maybe it brings up Frost and also Frasier because the both start with the "Fr". Another example would be wanting to just bring up all records that start with the letter "F" by simply searching for "F" instead of a complete name. Does that help? If you need me to provide anything else, let me know. Thanks for your help. Quote Link to comment https://forums.phpfreaks.com/topic/110002-using-dreamweaver-to-create-a-php-mysql-search-function/#findComment-566745 Share on other sites More sharing options...
fenway Posted June 16, 2008 Share Posted June 16, 2008 Well, SELECT * FROM PtInfo WHERE Last_Name LIKE 'Fr%' will work.... Quote Link to comment https://forums.phpfreaks.com/topic/110002-using-dreamweaver-to-create-a-php-mysql-search-function/#findComment-566787 Share on other sites More sharing options...
blackdog Posted June 18, 2008 Author Share Posted June 18, 2008 So if I understand correctly that is the specific command to bring back any names starting with Fr right? How do I set that up so that it does that with whatever is put in as the search criteria? Since it won't always be names beginning with Fr...I might be searching for patients with last names starting with St or S. I'm really new to php and SQL so I may be asking a dumb question. I guess part of my problem is that I don't understand what the variable '%s' is or where it comes from. I don't see it defined anywhere in my code. I think that if I understood how dreamweaver comes up with that variable I might be able to wrap my mind around it a little better. Quote Link to comment https://forums.phpfreaks.com/topic/110002-using-dreamweaver-to-create-a-php-mysql-search-function/#findComment-567883 Share on other sites More sharing options...
fenway Posted June 18, 2008 Share Posted June 18, 2008 the %s is a sprintf substitution marker... that is replaced with $colname_rsPtInfo... but then there's no "%" in the mysql query. You'd need: $query_rsPtInfo = sprintf("SELECT * FROM PtInfo WHERE Last_Name LIKE '\%%s' ORDER BY Last_Name ASC", $colname_rsPtInfo); Quote Link to comment https://forums.phpfreaks.com/topic/110002-using-dreamweaver-to-create-a-php-mysql-search-function/#findComment-568147 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.