Jump to content

Using dreamweaver to create a php mysql search function


blackdog

Recommended Posts

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);

?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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. 

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.