Searching PHP & MySQL: Roots, Prefixes, & Suffixes

In summary: Glad it works now!In summary, the conversation discusses a problem with a PHP code for a search engine on a database with one table and six fields. The goal is to display values from certain fields if they match the input from a search form. The issue is resolved by using the string concatenation operator in the SQL statement.
  • #1
klusener
62
0
Hi everyone,

I'm just beginning to learn php, but I'm working on a small script where I'm trying to search a few fields in a table and if the values match, other fields in the same row are displayed.

The db has just one table with 6 fields: ID (primary key), words, definitions, roots, prefixes, and suffixes.

I'm trying to make the search engine get the value from a search form, check it against roots, prefixes, and suffixes. If any of them equal the value, the values from words, and definitions need to be displayed.

But, for some reason it just doesn't work when I arrange the code like this:

$query = "SELECT words, definitions FROM Etym WHERE roots = '$trimm' OR prefixes = '$trimm' OR suffixes = '$trimm'";

I'm attaching both the search.php and the search form, if any of you would like to help me out of this mess. I would really appreciate any advice.

Thanks.
 

Attachments

  • searchform.txt
    410 bytes · Views: 301
  • search.txt
    5 KB · Views: 485
Technology news on Phys.org
  • #2
Not familiar with PHP, but doubt it would parse that string.
Chances are you need to use a string concatenation operater to insert your variables in the string.
Like
$query = "SELECT words, definitions FROM Etym WHERE roots = '" + $trimm + "' OR prefixes = '" +
 
  • #3
I think the string concatination operator in PHP is a period.

$query = "SELECT words, definitions FROM Etym WHERE roots = " . $trimm . " OR prefixes = " . $trimm . " OR suffixes = " . $trimm;
 
  • #4
j777 said:
I think the string concatination operator in PHP is a period.

$query = "SELECT words, definitions FROM Etym WHERE roots = " . $trimm . " OR prefixes = " . $trimm . " OR suffixes = " . $trimm;

It says : Unknown column 'ab' in 'where clause'

(ab was the input).
 
  • #5
Notice that j777 didn't put your ' in when he updated the statement. SQL does require the ' around literals.
 
  • #6
Thanks j777 and NoTime, it works like a charm.
 
  • #7
Oops, sorry about the missing single quotes.
 

Related to Searching PHP & MySQL: Roots, Prefixes, & Suffixes

1. What is PHP & MySQL?

PHP & MySQL are two popular programming languages used for creating dynamic and interactive websites. PHP is a server-side scripting language used for creating web pages, while MySQL is a database management system used for storing and managing data.

2. What are roots, prefixes, and suffixes in PHP & MySQL?

In PHP & MySQL, roots, prefixes, and suffixes are used to search for specific patterns in data. Roots refer to the beginning of a word or string, prefixes refer to the characters at the beginning of a word or string, and suffixes refer to the characters at the end of a word or string.

3. How are roots, prefixes, and suffixes used in searching PHP & MySQL databases?

Roots, prefixes, and suffixes are used in conjunction with the "LIKE" keyword in SQL queries to search for data that contains specific patterns. For example, the query "SELECT * FROM users WHERE name LIKE 'john%'" will return all users with names starting with "john".

4. Can roots, prefixes, and suffixes be combined in PHP & MySQL searches?

Yes, roots, prefixes, and suffixes can be combined in PHP & MySQL searches using the "%" wildcard. For example, the query "SELECT * FROM users WHERE name LIKE '%john%'" will return all users with names containing the string "john".

5. Are there any limitations to using roots, prefixes, and suffixes in PHP & MySQL searches?

There are some limitations to using roots, prefixes, and suffixes in PHP & MySQL searches. The "%" wildcard can only be used to represent one or more characters, so it cannot be used to search for specific patterns within a word. Additionally, the "LIKE" keyword is case sensitive, so searching for "John" will not return results for "john".

Similar threads

  • Programming and Computer Science
Replies
7
Views
549
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
4
Views
455
  • Programming and Computer Science
Replies
7
Views
6K
  • Programming and Computer Science
Replies
3
Views
3K
  • Programming and Computer Science
Replies
7
Views
5K
  • Programming and Computer Science
Replies
13
Views
4K
Replies
1
Views
3K
  • Programming and Computer Science
Replies
5
Views
3K
  • Programming and Computer Science
Replies
6
Views
3K
Back
Top