Jump to content

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";
?>

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.