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
Share on other sites

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

Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

$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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.