justlukeyou Posted March 26, 2011 Share Posted March 26, 2011 Hi, I have a piece of code which I trying to search two columns but I just cant get it to work. Instead it just dumps around 30 entries onto the page. Can anyone point me in the direction of how to search two columns? $query = "SELECT * FROM questions"; if(isset($_GET['question']) && !empty($_GET['notes'] )) { /*query the database*/ $question = $_GET['question']; $notes = $_GET['notes']; $query .= " WHERE question && notes like '%$question%' LIMIT 0, 10"; } $result = mysql_query($query); while($row = mysql_fetch_assoc($result)) { $question = $row['question']; $notes = $row['notes']; echo "$question $notes </br>"; } Link to comment https://forums.phpfreaks.com/topic/231802-searching-two-columns/ Share on other sites More sharing options...
nethnet Posted March 26, 2011 Share Posted March 26, 2011 Try this instead: WHERE `question` LIKE '%$question%' AND `notes` LIKE '%$notes%' LIMIT 0, 10 Edit: I'm assuming you want your `notes` field in your database to contain the $notes variable you defined before the SQL. Link to comment https://forums.phpfreaks.com/topic/231802-searching-two-columns/#findComment-1192642 Share on other sites More sharing options...
justlukeyou Posted March 26, 2011 Author Share Posted March 26, 2011 Hi, This echos the same contect. For some reason I cant get it to query anymore. Link to comment https://forums.phpfreaks.com/topic/231802-searching-two-columns/#findComment-1192645 Share on other sites More sharing options...
justlukeyou Posted March 26, 2011 Author Share Posted March 26, 2011 Edit: I'm assuming you want your `notes` field in your database to contain the $notes variable you defined before the SQL. Hi, I dont understand what you mean by this though. I have the column 'notes' in my database which I also want to search as I am currently only search the column 'question'. Link to comment https://forums.phpfreaks.com/topic/231802-searching-two-columns/#findComment-1192649 Share on other sites More sharing options...
silkfire Posted March 26, 2011 Share Posted March 26, 2011 Explain in words what you're trying to do and what to search for and in which columns. Are you searching for both notes and question in the question column or notes column or both columns? Link to comment https://forums.phpfreaks.com/topic/231802-searching-two-columns/#findComment-1192702 Share on other sites More sharing options...
justlukeyou Posted March 27, 2011 Author Share Posted March 27, 2011 Hi, I have two columns. One called 'Questions' which is similiar to the title of a forum post and another column called 'notes' which is similiar to the contect of a forum post. I have a script which searches the 'Questions' column which acts as the title. However, now I am now I am trying to search both the 'Questions' and 'Notes' columns. So the search results are for more useful than just searching the title. Link to comment https://forums.phpfreaks.com/topic/231802-searching-two-columns/#findComment-1192840 Share on other sites More sharing options...
justlukeyou Posted March 27, 2011 Author Share Posted March 27, 2011 Hi, Can anyone give me some advise please, I am trying to search both 'question' and 'notes'. $query = "SELECT * FROM questions"; if(isset($_GET['question']) && !empty($_GET['notes'] )) { /*query the database*/ $question = $_GET['question']; $notes = $_GET['notes']; $query .= "WHERE question && notes LIKE '%$question% LIMIT 0, 10"; } $result = mysql_query($query); while($row = mysql_fetch_assoc($result)) { $question = $row['question']; $notes = $row['notes']; echo "$question $notes </br>"; } if ($_GET['question'] == $question ) { echo 'Sorry, there are no questions or answers relating to your search. Please <a href="http://www.domain.co.uk/test/homesoeasy/phpaskquestion.php">ask a question</a>.'; } Link to comment https://forums.phpfreaks.com/topic/231802-searching-two-columns/#findComment-1192959 Share on other sites More sharing options...
silkfire Posted March 27, 2011 Share Posted March 27, 2011 $query = 'SELECT * FROM questions'; if (!empty($_GET['question']) && !empty($_GET['notes'])) $query .= ' WHERE question LIKE \'%' . mysql_real_escape($_GET['question']) . '%\' OR notes LIKE \'%' . mysql_real_escape($_GET['notes']) . '%\' LIMIT 0, 10'; $result = mysql_query($query); while (list($question, $notes) = mysql_fetch_row($result)) { echo $question, ' ', $notes, '<br>'; if ($_GET['question'] == $question ) echo 'Sorry, there are no questions or answers relating to your search. Please <a href="http://www.domain.co.uk/test/homesoeasy/phpaskquestion.php">ask a question</a>.'; } Link to comment https://forums.phpfreaks.com/topic/231802-searching-two-columns/#findComment-1193014 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.