Jump to content

Can't query MySQL if an ampersand is in the string?


suttercain

Recommended Posts

Hi everyone,

 

I have some comic books I am trying to query and some of the titles have and ampersand (&) symbol in them. The string cuts everything off after the ampersand.

 

Example:

I have a link that is 'Superman & Batman: Generation' which uses the get method to select the title via the WHERE title ='Superman & Batman: Generation', but I only get a result for Superman and everything else is cut off...

 

Any suggestions?

 

Thanks.

 

SC

I have a link that is 'Superman & Batman: Generation' which uses the get method to select the title via the WHERE title ='Superman & Batman: Generation', but I only get a result for Superman and everything else is cut off...

 

The problem is passing the ampersand in your URL en-encoded. try using the urlencode() before placing the title in the URL, and then use urldecode() on the GET variable to change the title back for your MySQL query. So your URL for that title will look like this after urlencode($title):

 

somepage.php?title=Superman+%26+Batman%3A+Generation

 

on somepage.php, get the 'real' title back using urldecode():

 

$real_title = urldecode($_GET['title']); // $real_title is back to "Superman & Batman: Generation"

 

try

<?php
if (isset($_GET['title']))
{
    $title = $_GET['title'] ;
    $sql = "SELECT * FROM comics WHERE title='$title'";
    echo $sql;
    echo "<hr />";
}

$title = urlencode("Superman & Batman");
$thispage = $_SERVER['PHP_SELF'];

echo "<a href='$thispage?title=$title'>Comic</a>";
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.