Jump to content

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


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>";
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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