Jump to content

mySQL partial match issues


emiller

Recommended Posts

Hi everyone,

 

I've finally been able to install a WAMP server and start rewriting and migrating all of my dynamic pages from asp/access to php/mySQL. I will warn you that i have to use Dreamweaver in  lot of this so that others in my department can edit what needs to be edited. My problem right now is that when I go to search the DB to find partial matches to the name barbera, (ie search: barb to find barbera) it returns all of the results in the DB.

 

I've included the code that works and returns full matches. What needs to change o include partial matches in both first name and last name?

 

<?php require_once('../../Connections/PhoneDirectory.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$varDept_PhoneDirectory = "%";
if (isset($_GET['Dept'])) {
  $varDept_PhoneDirectory = $_GET['Dept'];
}
$varFName_PhoneDirectory = "%";
if (isset($_GET['FirstName'])) {
  $varFName_PhoneDirectory = $_GET['FirstName'];
}
$varLName_PhoneDirectory = "%";
if (isset($_GET['LastName'])) {
  $varLName_PhoneDirectory = $_GET['LastName'];
}
mysql_select_db($database_PhoneDirectory, $PhoneDirectory);
$query_PhoneDirectory = sprintf("SELECT * FROM tblupdatephone WHERE Dept LIKE %s or FirstName LIKE %s or LastName LIKE %s ORDER BY FirstName,LastName", GetSQLValueString($varDept_PhoneDirectory, "text"),GetSQLValueString($varFName_PhoneDirectory, "text"),GetSQLValueString($varLName_PhoneDirectory, "text"));
$PhoneDirectory = mysql_query($query_PhoneDirectory, $PhoneDirectory) or die(mysql_error());
$row_PhoneDirectory = mysql_fetch_assoc($PhoneDirectory);
$totalRows_PhoneDirectory = mysql_num_rows($PhoneDirectory);
?>

Link to comment
https://forums.phpfreaks.com/topic/62850-mysql-partial-match-issues/
Share on other sites

Example I found online (http://www.webmasterworld.com/forum88/5627.htm):

SELECT * FROM blog_posts WHERE keywords LIKE '%design%' ORDER BY timestamp

 

Maybe just stick the variables in?

<?php
$query_PhoneDirectory = "SELECT * FROM tblupdatephone
WHERE Dept LIKE '%$varDept_PhoneDirectory%' OR
FirstName LIKE '%$varFName_PhoneDirectory%' OR
LastName LIKE '%$varLName_PhoneDirectory%'
ORDER BY FirstName, LastName";
?>

Archived

This topic is now archived and is closed to further replies.

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