FairyBusiness
asked on
How to search my whole database not just table?
Hi, I am creating a search bar and I want it to search my whole database so I wrote my query as follows:
$query = "SELECT * FROM auroriella LIKE '%$find%'";
but its not working. Anyone know how to do this?
$query = "SELECT * FROM auroriella LIKE '%$find%'";
but its not working. Anyone know how to do this?
ASKER
ok, I just wrote this:
Am I on the right track here? (bracelets, necklaces, and earrings are the tables I want to be searched)
$query = "SELECT * FROM bracelets";
$query .= "UNION SELECT * FROM necklaces";
$query .= "UNION SELECT * FROM earrings";
$query .= "WHERE MATCH * AGAINST '%$find%' ";
$result = mysql_query($query, $conn);
Am I on the right track here? (bracelets, necklaces, and earrings are the tables I want to be searched)
you need to repeat the WHERE condition for every table:
$query = "SELECT * FROM bracelets";
$query .= "WHERE MATCH * AGAINST '%$find%' ";
$query .= "UNION SELECT * FROM necklaces";
$query .= "WHERE MATCH * AGAINST '%$find%' ";
$query .= "UNION SELECT * FROM earrings";
$query .= "WHERE MATCH * AGAINST '%$find%' ";
$result = mysql_query($query, $conn);
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
It doesn't like the necklaces apparently lol
Query failed: 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 '* AGAINST '%BLUE%' UNION SELECT * FROM necklaces WHERE MATCH * AGAINST '%BLUE%' ' at line 1
Query failed: 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 '* AGAINST '%BLUE%' UNION SELECT * FROM necklaces WHERE MATCH * AGAINST '%BLUE%' ' at line 1
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I do have a table called products which has the names of all those tables.
I changed my code to this:
But I got this error message: (I searched for blue)
Query failed: 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 '* AGAINST 'BLUE' UNION ALL SELECT * FROM necklaces WHERE MATCH * AGAINST 'BLUE' ' at line 1
I changed my code to this:
$query = "SELECT * FROM bracelets ";
$query .= "WHERE MATCH * AGAINST '$find' ";
$query .= "UNION ALL SELECT * FROM necklaces ";
$query .= "WHERE MATCH * AGAINST '$find' ";
$query .= "UNION ALL SELECT * FROM earrings ";
$query .= "WHERE MATCH * AGAINST '$find' ";
But I got this error message: (I searched for blue)
Query failed: 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 '* AGAINST 'BLUE' UNION ALL SELECT * FROM necklaces WHERE MATCH * AGAINST 'BLUE' ' at line 1
ASKER
Ok, I changed it:
but its still screaming at me:
Query failed: 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 ''BLUE' UNION ALL SELECT * FROM necklaces WHERE MATCH (name, content, color) AGAI' at line 1
$query = "SELECT * FROM bracelets ";
$query .= "WHERE MATCH (name, content, color) AGAINST '$find' ";
$query .= "UNION ALL SELECT * FROM necklaces ";
$query .= "WHERE MATCH (name, content, color) AGAINST '$find' ";
$query .= "UNION ALL SELECT * FROM earrings ";
$query .= "WHERE MATCH (name, content, color) AGAINST '$find' ";
but its still screaming at me:
Query failed: 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 ''BLUE' UNION ALL SELECT * FROM necklaces WHERE MATCH (name, content, color) AGAI' at line 1
ASKER
Ok, I finally got a different error message:
Query failed: Can't find FULLTEXT index matching the column list
I searched just one table for simplicity:
How do make my fields FULL TEXT indexes??
Query failed: Can't find FULLTEXT index matching the column list
I searched just one table for simplicity:
$query = "SELECT * FROM bracelets ";
$query .= "WHERE MATCH (name, content, color) AGAINST ('$find') ";
How do make my fields FULL TEXT indexes??
>How do make my fields FULL TEXT indexes??
by creating a full text index on the table(s)
http://dev.mysql.com/doc/refman/5.0/en/create-index.html
by creating a full text index on the table(s)
http://dev.mysql.com/doc/refman/5.0/en/create-index.html
ASKER
I am still getting the same error message even though I altered my table columns
ALTER TABLE bracelets ADD FULLTEXT (name)
ALTER TABLE bracelets ADD FULLTEXT (name)
ASKER
Even in my phpmyadmin it lists them as FULL TEXT columns:
content FULLTEXT
color FULLTEXT
name FULLTEXT
What else can I do?
content FULLTEXT
color FULLTEXT
name FULLTEXT
What else can I do?
checklist:
* full text index added on all 3 tables, each containing the 3 columns
* the query syntax is validated to run on each table individually
* you post the most recent SQL you tried, along with the error message you get
...
* full text index added on all 3 tables, each containing the 3 columns
* the query syntax is validated to run on each table individually
* you post the most recent SQL you tried, along with the error message you get
...
ASKER
Well, I am just searching the bracelet table for now. I commented the rest outl
Error message:
Query failed: Can't find FULLTEXT index matching the column list
http://www.auroriella.com/results.php
$query = "SELECT * FROM bracelets ";
$query .= "WHERE MATCH (name, content, color) AGAINST ('$find') ";
/*$query .= "UNION ALL SELECT * FROM necklaces ";
$query .= "WHERE MATCH (name, content, color) AGAINST '$find' ";
$query .= "UNION ALL SELECT * FROM earrings ";
$query .= "WHERE MATCH (name, content, color) AGAINST '$find' "; */
$result = mysql_query($query, $conn); $result = mysql_query($query, $conn);
confirm_query($result);
Error message:
Query failed: Can't find FULLTEXT index matching the column list
http://www.auroriella.com/results.php
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Oh, well maybe I don't have that.
How do I create one full text index with 3 columns??
something like this:
ALTER TABLE bracelets ADD FULLTEXT (name, content, color)
How do I create one full text index with 3 columns??
something like this:
ALTER TABLE bracelets ADD FULLTEXT (name, content, color)
you might want to find the syntax in the above link I posted ...
ASKER
Ok, I fixed it and it works when I search one or two tables, but once I search all three it sends an error message. Can I use UNION ALL more than once?
$query = "SELECT * FROM bracelets ";
$query .= "WHERE MATCH (name, content, color) AGAINST ('$find') ";
$query .= "UNION ALL SELECT * FROM necklaces ";
$query .= "WHERE MATCH (name, content, color) AGAINST ('$find') ";
$query .= "UNION ALL SELECT * FROM earrings ";
$query .= "WHERE MATCH (name, content, color) AGAINST '$find' ";
Query failed: 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 ''PURPLE'' at line 1
$query = "SELECT * FROM bracelets ";
$query .= "WHERE MATCH (name, content, color) AGAINST ('$find') ";
$query .= "UNION ALL SELECT * FROM necklaces ";
$query .= "WHERE MATCH (name, content, color) AGAINST ('$find') ";
$query .= "UNION ALL SELECT * FROM earrings ";
$query .= "WHERE MATCH (name, content, color) AGAINST '$find' ";
Query failed: 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 ''PURPLE'' at line 1
ASKER
Nope, it was syntax error not the UNION ALL. Thanks for all of your help!!
also, you cannot search for all columns at one, but you have to repeat the condition (LIKE) for every column.
normally, you would implement this by doing
1) a UNION on all relevant tables to be searched for
2) putting a full text index on all relevant columns (for all tables involved) to make sure your query returns in a reasonable time
3) change the LIKE into a MATCH .. .AGAINST syntax to use the full-text index
hope this helps