Re: variable within regular expression
| From: | Robbie Staufer | Date: | Mon, 07 Apr 2003 21:30:34 +0000 |
| Subject: | Re: variable within regular expression | ||
| References: | 1 2 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-26913@lists.php.net to get a copy of this message | ||
Hi All,
Thanks for all your suggestions. I've tried them all and am still having no luck. It may be the combination of how I'm using the conversion statement and the query statement. I've been checking through books and on the web and am not finding much about how to use REGEXP in a query, where the value of the expression is taken from user input and stored in a variable. (I'm thinking it should be exactly as Ignatius says....) Here's what I have now;
// user enters a partial expression in the $con form field, and php converts the
// input to a regular expression before searching the database for matches
//convert $con to regular expression
// $con = "%$con%";
$query = "SELECT {$field} FROM {$table} WHERE {$variable} REGEXP '{$con}' ";
The results page from this search shows no matches.
Thanks for your help and patience.
Robbie
Ignatius Reilly wrote:
For readability (and writability...) purpose, I prefer to write such a query in the form: $query = "SELECT {$field}FROM {$table}WHERE {$variable}REGEXP '{$con}' " ; Ignatius ____________________________________________ ----- Original Message ----- From: <Jeffrey_N_Dyke@Keane.com> To: "Robbie Staufer" <rstaufer@scd.ucar.edu> Cc: <php-db@lists.php.net>; "Rob Bryant" <rob@3jar.com> Sent: Monday, April 07, 2003 7:25 PM Subject: Re: [PHP-DB] variable within regular expression-- -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* Robbie Staufer NCAR/SCD 1850 Table Mesa Dr. Rm. 42 Boulder, CO. 80305 (303) 497-1836if you want to truley use regular expressions, change the query to $query = "SELECT " . $field . " FROM " . $table . " WHERE " . $variable . " REGEXP '".$con."'"; and drop the %'s, as you won't need them with a true REGEXP search. as far as the signle quotes those should be fine. but in this example i concatenated with -> . this could be nothing like what your looking for...and if so, sorry for th waste of bandwidth. jdvariable within regular expressionRobbie Staufer <rstaufer@scd. To: Rob Bryant <rob@3jar.com> ucar.edu> cc: php-db@lists.php.net Subject: Re: [PHP-DB]04/07/2003 01:19 PMThanks for the help. Here's what I tried:// convert $con to regular expression $con = "%$con%"; $query = "SELECT " . $field . " FROM " . $table . " WHERE " .$variable . " LIKE '%{$con}%'"; I'm sure this is close to correct, but it's not working yet. The application itself works for numeric values for $con, and also for exact string values for $con. But if a partial string is entered for $con, the regex conversion part fails. Thanks, Robbie Staufer Rob Bryant wrote:variable.-----Original Message----- From: Robbie Staufer [mailto:rstaufer@scd.ucar.edu] Sent: Friday, April 04, 2003 3:59 PM To: php-db@lists.php.net Subject: [PHP-DB] variable within regular expression I have a php query form in which the user input must be converted to a regular expression before querying the db. Something like this pseudo code:I think maybe the single quotes don't interpolate the value of the$var = $_GET['usr-input']; $var = '%$var%';SELECT * FROM table WHERE field LIKE $var. I can't find the right syntax to make this work. Does anyone know?In other words, let's say $var is 'fubar': $var = '%$var%'; // -> is literally %$var% vs. $var = "%$var%"; // -> is %fubar% Anyway, here's some of my code that is similar to (what I think) you're trying to do: if (isset($_GET['nstring'])) { $nstring = addslashes($_GET['nstring']); $squery = "SELECT * FROM lead-- -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* Robbie Staufer NCAR/SCD 1850 Table Mesa Dr. Rm. 42 Boulder, CO. 80305 (303) 497-1836 -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.phpWHERE name LIKE '%{$nstring}%' ORDER BY source, ref_by, date DESC";$sresult = mysql_query($squery); } // etc...