RE: [PHP-DB] variable within regular expression
| From: | Rob Bryant | Date: | Sat, 05 Apr 2003 01:46:53 +0000 |
| Subject: | RE: [PHP-DB] variable within regular expression | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-26844@lists.php.net to get a copy of this message | ||
> -----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:
> $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?
>
I think maybe the single quotes don't interpolate the value of the variable.
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
WHERE name LIKE '%{$nstring}%'
ORDER BY source, ref_by, date DESC";
$sresult = mysql_query($squery);
} // etc...