Passing MySQL Wildcard into URL

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I have a search feature where users can search a database. All works
very well except for one small annoying bug.

If a user searches for anything using wildcards and the '%' is the
FIRST character searched for, my pagination routine doesn't pass the
correct value.

Say the user enters %300. The page displays the correct data for the
first page, indicates that it is page 1 of 11 and when the NEXT link is
clicked, the value changes to 00. I haven't figured out why the %3
drops, however I suspect it may be my use of quotes. Particular code is
below. Can anyone help me?

Platform is Apache on FC3, MySQL and PHP. Browser being used doesn't

if($pageno == 1) {
echo "<TD CLASS=upperLinks>FIRST&nbsp;</TD><TD
echo "<TD CLASS=data1>FIRST&nbsp;</TD><TD CLASS=data1>PREV&nbsp;</TD>";
} else {
echo "<TD CLASS=data1>  <A
$prevpage = $pageno - 1;
echo "<TD CLASS=data1>  <A
<TD CLASS=data1>(Page <?=$pageno?> of <?=$lastpage?>)&nbsp;</TD>
if ($pageno == $lastpage) {
echo "<TD CLASS=data1>NEXT&nbsp;</TD><TD CLASS=data1>LAST&nbsp;</TD>";
} else {
$nextpage = $pageno + 1;
echo "<TD CLASS=data1>  <A
echo "<TD CLASS=data1>  <A



Re: Passing MySQL Wildcard into URL

Try urlencode() for the $searchfor variable. The reason why this is
happening is that %300 is converted before your script get to use it
like if it was used by urldecode().

Re: Passing MySQL Wildcard into URL

I'll try that. The weird thing is that if I echo the 'searchfor'
variable it contains the correct value:

$nextpage = $pageno + 1;
echo $searchfor;
echo "<TD CLASS=data1>  <A


Re: Passing MySQL Wildcard into URL

I may be wrong, but I think the problem is that, in URLs, % is a scape
character. For example, if you want to reference a remote file with
spaces on it's name (like "foo"), in your URL you use %20
(like ""). So the browser is simply misunderstanding your
"%" character.

Yous should not pass it. You can for example parse the criteria entered
by the user instead, eliminate the "%" of the variable and add a new
parameter to the URL, a flag that indicates that this concrete search
will use "%" at the beginning of the criteria. Add it programatically
to your SQL query and there you go.

redneck_kiwi wrote:
Quoted text here. Click to load it
Quoted text here. Click to load it

Re: Passing MySQL Wildcard into URL

That's what I meant. He can eliminate the problem by using urlencode().
As he said the first page works, most likely cause the search parameter
is passed on by HTTP POST requests, that urlencodes and decodes

Re: Passing MySQL Wildcard into URL

I used the code below to try and zero in on where the problem was:

$nextpage = $pageno + 1;
echo '<TD CLASS=data1><A HREF='.$link;

The '%' is definately where the problem is. I then tried doing a
URLENCODE/URLDECODE with the same results. The browser sees the % as a
start to a special character. Guess I'll have to do a switch statement
and if the user enters a leading % then deal with it properly.

Thanks everyone.


Site Timeline