suttercain Posted September 29, 2007 Share Posted September 29, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/71174-cant-query-mysql-if-an-ampersand-is-in-the-string/ Share on other sites More sharing options...
LemonInflux Posted September 29, 2007 Share Posted September 29, 2007 Why not just not use a & sign for these? Superman_and_Batman? Quote Link to comment https://forums.phpfreaks.com/topic/71174-cant-query-mysql-if-an-ampersand-is-in-the-string/#findComment-358001 Share on other sites More sharing options...
rarebit Posted September 29, 2007 Share Posted September 29, 2007 In HTML & is also & and & Quote Link to comment https://forums.phpfreaks.com/topic/71174-cant-query-mysql-if-an-ampersand-is-in-the-string/#findComment-358007 Share on other sites More sharing options...
rarebit Posted September 29, 2007 Share Posted September 29, 2007 Hmmm they might also pose a problem, now I think about it! Quote Link to comment https://forums.phpfreaks.com/topic/71174-cant-query-mysql-if-an-ampersand-is-in-the-string/#findComment-358009 Share on other sites More sharing options...
LemonInflux Posted September 29, 2007 Share Posted September 29, 2007 I still think you should just use 'and' instead of '&'. It won't affect the page, you just have to re-code a bit. Quote Link to comment https://forums.phpfreaks.com/topic/71174-cant-query-mysql-if-an-ampersand-is-in-the-string/#findComment-358022 Share on other sites More sharing options...
Barand Posted September 29, 2007 Share Posted September 29, 2007 try <?php $title = urlencode("Superman & Batman"); echo "<a href='?title=$title'>S&B</a> "; ?> Quote Link to comment https://forums.phpfreaks.com/topic/71174-cant-query-mysql-if-an-ampersand-is-in-the-string/#findComment-358029 Share on other sites More sharing options...
BlueSkyIS Posted September 29, 2007 Share Posted September 29, 2007 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" Quote Link to comment https://forums.phpfreaks.com/topic/71174-cant-query-mysql-if-an-ampersand-is-in-the-string/#findComment-358037 Share on other sites More sharing options...
Barand Posted September 29, 2007 Share Posted September 29, 2007 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>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/71174-cant-query-mysql-if-an-ampersand-is-in-the-string/#findComment-358042 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.