Jump to content

[SOLVED] "WHERE expr REGEXP pat" Creating Unexpected Error


labiere

Recommended Posts

I am trying to select a subset of rows from a table based on a text pattern.

My table column, labeled "id", has entries like this:

 

  • xx2
  • xx38
  • xxy2

(These items are always made up of 1 or more letters followed by one or more numbers)

 

I have defined a PHP variable $prefix="xx"

I want to perform a query that will select the two "xx" rows, but not the "xxy" row.

 

When I try the query like this, I get all three rows back, as I would expect:

// Performing SQL query
$query2 = "SELECT * FROM table WHERE id REGEXP '^$prefix'";

 

I thought this change would give me rows that start with "xx" immediately followed by a digit:

// Performing SQL query
$query2 = "SELECT * FROM table WHERE id REGEXP '^$prefix[0-9]'";

 

.. What I get with that change is an error message: Parse error: syntax error, unexpected '-', expecting ']'

 

Been banging my head over it for a while now.. Any ideas?

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.