0

I am trying to find a match in two different columns using input from two different form fields in a PHP form. I am referencing the two string variables that are input into the form in my mysql code, but it is returning an error:

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 (abcdef) AGAINST name' at line 1

I have tried a few variations of the syntax, but with no success.

$name = mysql_real_escape_string($_POST['studentname']);
$num = mysql_real_escape_string($_POST['studentnum']);
$qw = "SELECT name FROM students MATCH ($name) AGAINST name";
$qw1 = "SELECT studentnum FROM students MATCH ($num) AGAINST studentnum";
$namematch = mysql_query($qw) or die(mysql_error());
$nummatch = mysql_query($qw1) or die(mysql_error());

if (($namematch || $nummatch) == FALSE) {
    die('Name or student number do not match those on record');
}
faintsignal
  • 1,828
  • 3
  • 22
  • 30
Graham
  • 35
  • 6
  • What error is it returning? (Put this info in your question.) – faintsignal Apr 16 '14 at 02:50
  • 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 (abcdef) AGAINST name' at line 1 – Graham Apr 16 '14 at 02:53
  • [Why shouldn't I use mysql_* functions in PHP?](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) – Phil Apr 16 '14 at 02:55

3 Answers3

1

I believe you have your parameters backwards in the MATCH clause. You have:

$qw = "SELECT name FROM students MATCH ($name) AGAINST name";

Instead it should be:

$qw = "SELECT name FROM students MATCH (name) AGAINST $name";

In other words, MATCH (column) AGAINST (parameter), not the other way around.

http://dev.mysql.com/doc/refman/5.0/en/fulltext-search.html#function_match

LeifMasrud
  • 11
  • 1
0

First off, you are missing WHERE in your SQL statements.

Second, you have your arguments in MATCH and AGAINST reversed. MATCH should be given the column names as arguments.

E.g.

SELECT name FROM students MATCH ($name) AGAINST name

should be

SELECT name FROM students WHERE MATCH (name) AGAINST ('$name')

and similarly adjust your second query.

faintsignal
  • 1,828
  • 3
  • 22
  • 30
  • I tried correcting the syntax as you described, but it is still returning the same error. – Graham Apr 16 '14 at 03:00
  • @Graham See edit. Does this resolve? I'm seeing conflicting info about quotes in the AGAINST param. – faintsignal Apr 16 '14 at 03:43
  • I tried what you specified in the edit, and made sure that everything is compatible with full text search. Now I am getting the error "Unknown column ".." in where clause" – Graham Apr 16 '14 at 13:28
  • Well I assume `name` and `studentnum` are column names in your `students` table. So I'm stumped otherwise. – faintsignal Apr 17 '14 at 03:00
0

try this, use in_array to match array:

while($row = mysqli_fetch_array($qw)){
$namematch[] = $row['name'];
}

while($row2 = mysqli_fetch_array($qw)){
$nummatch[] = $row['studentnum'];
}

if(in_array($name,$namematch)){
echo 0;
}else{
echo 1;
}

*NOTE Please avoid to use mysql. I suggest use MYSQLi rather than use MYSQL. Just FYI.

user3517652
  • 94
  • 1
  • 8