views:

30

answers:

1

i'm trying to search for multiple columns using this code:

 <?php

 // Connection Database
 $search = $_POST ['Search'];

mysql_connect("xxxxxx", "xxxxxx", "xxxxx") or die ("Error Connecting to Database");
mysql_select_db("xxxxx") or die('Error');
    $data = mysql_query("SELECT CourseName, CourseDescription, CourseLeader FROM course   MATCH (CourseName, CourseDescription, CourseLeader) AGAINST ('". $search ."')
or die('Error');
Print "<table border cellpadding=3>";
while($info = mysql_fetch_array( $data ))
 {
  Print "<tr>";
  Print "<th>Course Name:</th> <td>".$info['CourseName'] . "</td> ";
  Print "<th>Course Description:</th><td>".$info['CourseDescription'] . "</td> ";
  Print "<th>Course Leader:</th><td>".$info['CourseLeader'] . " </td></tr>";

  }
  Print "</table>";

   ?>

i'm getting the following error: Parse error: syntax error, unexpected T_STRING in /home/a7105766/public_html/website/scripts/coursesearchdb.php on line 30

what am I doing wrong??

cheers

+3  A: 

This line is incorrect:

$data = mysql_query("SELECT ... AGAINST ('". $search ."') or die('Error');

You can even see the error from the syntax highlighting that Stack Overflow uses. Do you use syntax highlighting when developing? If not, I'd recommend it for catching exactly this sort of error.

The solution - you need to close the double-quote and the open parenthesis.

$data = mysql_query("SELECT ... AGAINST ('". $search ."')") or die('Error');

Also instead of die('Error') you could write something useful such as die(mysql_error()). You could also look at trigger_error(mysql_error()).

In your SQL you are missing the keyword WHERE. See Full-Text Search Functions in the manual for more information on full text searches.

Mark Byers
im getting 'error' now...
addi
@addi: See my update: Don't use `die('Error')` - that's incredibly unhelpful for debugging. Change it to `die(mysql_error())`. This should give you the error message for your query, which should hopefully allow you to find the error yourself.
Mark Byers
im getting this now: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'MATCH (CourseName, CourseDescription, CourseLeader) AGAINST ('')' at line 1 no idea what it means!! is MATCH wrong?
addi
@addi: Use `WHERE MATCH`.
Mark Byers
i added WHERE to MATCH and now i'm getting this error message: Can't find FULLTEXT index matching the column list
addi
@addi: Well I think that message is quite self-explanatory... you don't have a full text index for one of the columns.
Mark Byers
i'm sorry but i'm really new at this, do I have to add this fullindex search to my table? I don't quite understand.cheers
addi
ah no worries, ALTER TABLE course ADD FULLTEXT(CourseName, CourseDescription, CourseLeader); seems to have done the trick. I have learnt a lot tonight, thanks for all your help @Mark Byers
addi
another thing while im here, one of my columns is quite long, but in the results it doesnt show it fully, how can I acheive this??
addi
@addi: Post it as a question, preferably with more information than just one line.
Mark Byers
in my php code I print the columns CourseName, CourseDescription, CourseLeader after a search, as a resultset. CourseDescription has a lot of text, how do I print it all? is there a way to change the column widths?
addi
@addi: Your question is completely unrelated to this question. Create a new question. Click here: http://stackoverflow.com/questions/ask
Mark Byers
ah ok I was just saving space!
addi