0

again Experts,

I am grateful to have gotten the help of Jason Sperske with the regex below:

$pattern = "/(\d{2})([ND]?)(\d*)(GG[\d]*)?/";

function format($matches) {
  return $matches[1][0].(strlen($matches[2][0])>0?$matches[2][0]:" ").$matches[3][0].(strlen($matches[4][0])>0?"  ".$matches[4][0]:"");
}

The regex helps format values entered for an ID field.

For instance, if a user enters 12343GG90494, the regex will insert 2 spaces before the GG thereby producing the following output 12343 GG90494.

This is just one example of how the regex formats user input.

This is important because users of our apps often don't put the 2 extra spaces and a result, their searches produce no results.

The regex above solves that problem for us.

My issue right now is to use the regex with the following sample query:

$tid = $_GET["tid"];

// Connect to SQL Server database
include("../connections/TDConnect.php");

 $tsql = "SELECT * FROM TC(dtops.dbo.tSearch, Name, '\"$tid*\"')";

How do I use it with the input param called $tid?

Your assistance, as always, is greatly appreciated.

1 Answer 1

1

Without access to the TDConnect.php its hard to know exactly what you need so this advice is generic (well i guess MySql specific but not that much so). Use prepared statements! Specifically for this problem:

$mysqli = new mysqli("example.com", "user", "password", "database");
if ($mysqli->connect_errno) {
  echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
/* Prepared statement, stage 1: prepare */
if (!($stmt = $mysqli->prepare("SELECT * FROM Table WHERE Col LIKE (?)"))) {
  echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;
}
if (!$stmt->bind_param("s", $tid)) {
  echo "Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error;
}

if (!$stmt->execute()) {
    echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks again for coming to the rescue. I can see that I didn't make myself very clear. First, I am using SQL Server db. Second, I have the query that I need already. I just want to know how to integrate your regex with this: $tid = $_GET["tid"]; such that when I pass a variation of those IDs we worked on, we can get the correct format. Example, is this what I would need? $tid = $_GET["tid"]; format($tid)???. The format being the function you wrote?
You can use this '$tid = format($_GET["tid"]);' to go from query string parameter to PHP formatted variable. As for the SqlServer part, sorry I missed that. I hope this helps, good luck getting your code to work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.