PHP -- MySQL Wildcard

I was trying to get the wildcard working in a SQL statement on a MySQL database but it doesn’t seem to work. I am using it in the following way:

  $query = "Select * From Details
                Where Name Like '$FirstName%'";

I tried using equals (=) instead of like and * but still didn’t work. I have another problem if anybody could offer any help. It relates to adding parameters to a URL that is not on the HTML form but it being added as a static value by the page. It might be a HTML problem but am not sure how to get around it:

      <form action="example.php" method="GET">

I want to add a value to the file in the form of example.php?var=2 but they page is also adding a variable so really I want the page to add it’s variable and add my own as well.


The % acts as a wildcard by itself.

“select * from tblname where column …”
“… like ‘%name’” will return anything that ends with ‘name’.
“… like ‘name%’” will return anything that begins with ‘name’.
“… like ‘%name%’” will return anything that has ‘name’ is the string.
“… like ‘%%’” will return every row in the table.
“… like na%me will return anything that starts with ‘na’ and ends with ‘me’” (actually I’m not sure about this one, but it seems like it should work).

I’m not sure what you’re other problem is. It sounds like you’re trying to pass values that are not part of the form. I usually use a hidden form tag, which works the same as a normal form tag.

If you haven’t already found an answer, then try this:

$query = “Select * From Details
Where Name Like “.'$FirstName.”%'”;

I think that the problem is that the $FirstName variable is not resolving properly since it is within single quotes inside double quotes (which becomes a problem when you have magic quotes turned on). If I’m right, the following lines should print two different results:

$query = “Select * From Details Where Name Like ‘$FirstName%’”;
echo $query;
$query = “Select * From Details Where Name Like “.'$FirstName.”%'”;
echo $query;

I hope this helps.

Also, to be safe, you should make sure that yo0ur $FirstName var is being trim()med. That’s a common error that’s caused me no end of extra debugging hours.

I know this is a really elementary tip, but I’ve found that the more elementary problems cost me more hours than the complex ones do.