Jump to content

Searching Two Columns


justlukeyou

Recommended Posts

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

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'.

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.

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>.';
}

$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>.';
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.