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 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? 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 & 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! 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. 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> "; ?> 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" 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>"; ?> 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
Archived
This topic is now archived and is closed to further replies.