stublackett Posted February 8, 2008 Share Posted February 8, 2008 Hi, I'm looking to 'search' my database to find data that corresponds to the field the user has searched Its down to the SELECT query, I believe which is not executing the text inputted into the box The code is as follows : <form action="" method="get" name="find" id="find"> <fieldset> <legend>Search Users....</legend> <div> <label for="firstname">First Name</label> <input type="text" name="firstname" id="firstname" class="txt" /> <input type="submit" value="Show Users"> </div> </form> <?php $hostname = "localhost"; $db_user = "root"; $db_password = ""; $dbname = "test"; $db_table = "dummydata"; $db = mysql_connect($hostname, $db_user, $db_password); mysql_select_db($dbname,$db); $selection = $_GET['firstname']; if(! $db ) { die('Could not connect: ' . mysql_error()); } $sql = 'SELECT * FROM dummydata WHERE "$selection"'; mysql_select_db('dummydata'); $retval = mysql_query( $sql, $db ); if(! $retval ) { die('Could not get data: ' . mysql_error()); } while($row = mysql_fetch_array($retval, MYSQL_NUM)) { echo "firstname :{$row[0]} <br> ". "secondname : {$row[1]} <br> ". "--------------------------------<br>"; } mysql_free_result($retval); echo "These are the users you found in the Database"; ?> I've echoed the $sql and it says "DBSELECT * FROM dummydata WHERE "$selection"All All being the search title I put into my DB Any idea how to get this to replace all with the $selection ? Quote Link to comment https://forums.phpfreaks.com/topic/90079-user-driven-queries-in-php/ Share on other sites More sharing options...
revraz Posted February 8, 2008 Share Posted February 8, 2008 Variables in strings require double quotes. $sql = "SELECT * FROM dummydata WHERE username='$selection'"; You also forgot the field name to compare to. And if you want to search for say Rob and have it return Rob and Robert, then you need to use LIKE as well as % wildcard. Quote Link to comment https://forums.phpfreaks.com/topic/90079-user-driven-queries-in-php/#findComment-461849 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.