Searching PHP & MySQL: Roots, Prefixes, & Suffixes

  • Context: PHP 
  • Thread starter Thread starter klusener
  • Start date Start date
  • Tags Tags
    Mysql Php Search
Click For Summary

Discussion Overview

The discussion revolves around a PHP script intended to search a database table for matches in specific fields (roots, prefixes, and suffixes) and display corresponding values from other fields (words and definitions). The focus is on troubleshooting the SQL query construction and syntax issues encountered by the original poster.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • The original poster describes an issue with their SQL query not functioning as expected when searching for values in the database.
  • One participant suggests that the original poster may need to use a string concatenation operator to properly insert variables into the SQL string.
  • Another participant confirms that the correct string concatenation operator in PHP is a period and provides a revised version of the SQL query.
  • A later post points out an error in the SQL query related to missing single quotes around string literals, which is necessary for SQL syntax.
  • The original poster acknowledges the correction and confirms that the revised query works correctly.

Areas of Agreement / Disagreement

Participants generally agree on the need for proper syntax in SQL queries, particularly regarding string concatenation and the use of quotes around literals. However, there is no explicit consensus on broader issues beyond the immediate technical corrections.

Contextual Notes

Limitations include potential misunderstandings of SQL syntax and PHP string handling, as well as the specific context of the database structure and input values.

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

  • · Replies 7 ·
Replies
7
Views
5K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 7 ·
Replies
7
Views
7K
  • · Replies 7 ·
Replies
7
Views
6K
  • · Replies 5 ·
Replies
5
Views
4K
Replies
6
Views
4K
  • · Replies 4 ·
Replies
4
Views
7K
Replies
1
Views
4K
  • · Replies 3 ·
Replies
3
Views
4K
Replies
4
Views
3K