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

  • Thread starter Thread starter klusener
  • Start date Start date
  • Tags Tags
    Mysql Php Search
AI Thread Summary
A user is learning PHP and is attempting to create a script that searches a database table with fields for ID, words, definitions, roots, prefixes, and suffixes. The goal is to retrieve words and definitions based on user input that matches any of the roots, prefixes, or suffixes. The initial query did not work due to incorrect string concatenation and missing single quotes around the input values. Correcting the syntax to use the period for concatenation and ensuring single quotes are included around the variables resolved the issue, allowing the script to function properly.
klusener
Messages
61
Reaction score
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

Technology news on Phys.org
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 = '" +
 
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;
 
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).
 
Notice that j777 didn't put your ' in when he updated the statement. SQL does require the ' around literals.
 
Thanks j777 and NoTime, it works like a charm.
 
Oops, sorry about the missing single quotes.
 

Similar threads

Back
Top