forumnz Posted February 3, 2011 Share Posted February 3, 2011 I have an AJAX script which queries a DB when a user inputs a search text. That's great, works well. However, when I use mysql_real_escape_string, it seems to completely rid the '$q'. When I don't use it, it works well but of course, there's the security side of things. Here's the code snippet: <?php error_reporting(E_ALL); $q = $_GET["q"];//added mysql_real_escape_string //$q = mysql_real_escape_string('$_GET["q"]'); $con = mysql_connect("localhost","aaaaa","aaaaa"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("aaaaa", $con); $sql="SELECT * FROM articles WHERE keywords LIKE '%$q%'"; ?> It seems so simple, but its just not working. What could it be? Quote Link to comment https://forums.phpfreaks.com/topic/226624-problem-using-mysql_real_escape_string/ Share on other sites More sharing options...
QuickOldCar Posted February 4, 2011 Share Posted February 4, 2011 Use it like this $q=mysql_real_escape_string($_GET['q']); Quote Link to comment https://forums.phpfreaks.com/topic/226624-problem-using-mysql_real_escape_string/#findComment-1169647 Share on other sites More sharing options...
forumnz Posted February 4, 2011 Author Share Posted February 4, 2011 Thanks but it just selects everything. As if $q is empty. I know for a fact that it shouldn't select most of the rows it does. What else could it be? Quote Link to comment https://forums.phpfreaks.com/topic/226624-problem-using-mysql_real_escape_string/#findComment-1169648 Share on other sites More sharing options...
Pikachu2000 Posted February 4, 2011 Share Posted February 4, 2011 Connect to the database before using mysql_real_escape_string(), then try it again. Quote Link to comment https://forums.phpfreaks.com/topic/226624-problem-using-mysql_real_escape_string/#findComment-1169651 Share on other sites More sharing options...
forumnz Posted February 4, 2011 Author Share Posted February 4, 2011 Very nice. Works now Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/226624-problem-using-mysql_real_escape_string/#findComment-1169652 Share on other sites More sharing options...
Pikachu2000 Posted February 4, 2011 Share Posted February 4, 2011 Hold on a minute. Look at your query. SELECT * FROM articles WHERE keywords LIKE '%$q%' That query says "SELECT all fields FROM table WHERE keywords [contains the value in $q anywhere, regardless of its position in the string]" Is that what you mean for it to say? Quote Link to comment https://forums.phpfreaks.com/topic/226624-problem-using-mysql_real_escape_string/#findComment-1169654 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.