Problem with & + Database

Hello
I’m using PHP to insert names to my databae and some of the names contains “&” symbol.
When Im trying to access the name page (like mysite.com/hocky&tocky) it cannot find the specific name from the database (it does not recognize hocky&tocky).
How I can fix that and still show the “&” symbol?

Replace & with & or &

Look at the urlencode and urldecode functions.

I have a name in the database: S&M
when im trying to use $_GET to get it from the url for some reason it get me only “S”
tried with and without decode

Why did you not bother to take his advice and use the two functions: urlencode() and urldecode()? Had you have done that, you’d have found the solution to your problem.

Here’s a quick demonstration of their usage:


<?php

$name = 'S&M';

$nameEncoded = urlencode($name);

echo '<a href="get.php?name=', $nameEncoded, '">Click Me</a>';

var_dump(urldecode($_GET['name']));

it makes the url fine but my problem is
when im trying to access the $_GET variable in the encoded/decoded url it returns only “S”… I have no idea why

When you urlencode the value in the URL then you will get the correct value in $_GET - there no other possibility! If you have doubts then please post your URL here so we can see if it’s been poperly encoded.

BTW, tpunt made a small mistake in his code:

var_dump([COLOR="#FF0000"]urldecode[/COLOR]($_GET['name']));

You don’t use urldecode on $_GET variables because they are already decoded by the server! Just access $_GET[‘name’] directly and that’s it. Only urlencode is needed here for encoding the URL.

I said I was trying to print the $_GET with and without the encode, both returns me “S”, i have no idea why…
all other gets without the “&” works

How you print GET is not the main problem. The problem is how you encode the URL. Please post the URL.

In your example

mysite.com/hocky&tocky

the name “hocky&tocky” is a folder name, not a GET request.
You have to create a valid link to access the GET variable, unless you have rewrite options.

So, as you can read above, you need a link like this:

  1. http://mysite.com?hocky%26tocky // in case you have a special rewrite rule
    OR
  2. http://mysite.com?name=hocky%26tocky // in case you use classic GET

if you access this link, the server will know about:

  1. $_GET[‘Whatever_Rewrite_Var_You_Have’]
    OR
  2. $_GET[‘name’]

In my example, %26 is the same thing with urlencode(‘&’) so, to create the link you will need, as tpunt wrote, something like this

 <a href="get.php?name=<?php echo urlencode('hocky&tocky') ?>">Click Me</a> 

urlencode is used to create every link query.
Check the manual for details.

Please be aware if url rewrite is being used (Apache server) then mysite.com/hocky&stocky may be automatically transcoded to mysite.com/index.php?name=hocky&stocky

If the OP is using url rewrite then (s)he needs to check that also.

Thanks for the information, Lemon Juice. I was not aware of that.

But it’s important to note that auto-decoding by the server only applies to GET variables. If you want to get the value directly from $_SERVER[‘REQUEST_URI’] then the URL is not auto-decoded, we get the literal string from the browser address bar so in such cases both urlencode and urldecode are needed.

If URL rewriting is on then it may still be another case depending on how the URL is rewritten. If the OP uses URL’s like mysite.com/hocky&tocky then most probably he must be using some form of mod_rewrite. In such a case we would need to see the .htaccess file with the rules to see where the problem is.

this is the .htaccess part of the url

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule hear/(.+)$ index.php?hear&music=$1 [L]

So now I can see where your problem is. If you have a URL like example.com/hear/hocky&stocky it becomes translated by your rule to:

example.com/index.php?hear&music=hocky&stocky

Can you see now what is going on? The & sign is the delimiter of GET variables so in effect you receive these 3 GET variables in PHP:

  1. hear (empty value)
  2. music=hocky
  3. stocky (empty value)

The solution is to instruct mod_rewrite to encode your variable before it gets to PHP. This can be achieved by using the B flag that is available since Apache 2.2:

RewriteRule hear/(.+)$ index.php?hear&music=$1 [L,B] 

You may notice that your URL’s with special characters work without encoding. Technically such URL’s are incorrect but most browsers will do the encoding for you, hence they will still work but for maximum compatibility it is good practice to always use urlencode when creating links like shown in earlier posts in this thread.

Awesome! it works, thanks.