phpnoob80 Posted February 25, 2007 Share Posted February 25, 2007 hello, hopefully i am right in posting here as the error seems to be php related rather than MySQL and its not a PHP REGEXP. I am writing a script that will eventually search a db for post code prefixes, ie SO, WD. for certain Postcodes such as some London areas (E,L etc) i am using a query including mysql regexp to look for post codes that begin with a letter but followed by 1 or 2 numbers. LIKE won't work as if you look for L you will also get Leicester postcodes etc. Anyway, using phpmyadmin's SQL facility I have created the following query, tested it and it worked fine SELECT `postalcode` FROM `pcode` WHERE `postalcode` REGEXP '^[a-z]{1}[0-9]{1,2}' using phpmyadmin again, it generated the following php code of the above which i then pasted into my php script $pcodesearch=mysql_query('SELECT `postalcode` FROM `pcode` WHERE `postalcode` REGEXP \ '^[a-z]{1}[0-9]{1,2}\' LIMIT 0, 30 ')or die(mysql_error()); But when i run the script, i get a parse error as follows Parse error: parse error, unexpected '[' on line 31 line 31 is the line that has the query. am i not escaping something? it's got me stumped because the query works in normal SQL which is normally my main problem. if anyone can shed some light that would be very much appreciated. thank you for your time jj Quote Link to comment Share on other sites More sharing options...
magic2goodil Posted February 25, 2007 Share Posted February 25, 2007 Perhaps instead you could do: $pcodesearch=mysql_query('SELECT `postalcode` FROM `pcode` WHERE `postalcode` LIMIT 0, 30 ')or die(mysql_error()); $myArray = array(count(mysql_fetch_assoc($pcodesearch))); $i = 0; while($data = mysql_fetch_assoc($pcodesearch)) { foreach($data as $row) { if(!empty(preg_match($row, '^[a-z]{1}[0-9]{1,2}')) { $myArray[$i] = preg_match($row, '^[a-z]{1}[0-9]{1,2}'); $i++; } else { $i++; } } } I didn't test this yet, so forgive em if it's not right on. Quote Link to comment Share on other sites More sharing options...
phpnoob80 Posted February 25, 2007 Author Share Posted February 25, 2007 hello thank you for replying. i will try your code, but i am interested in how to fix the error too. being a novice it took me a while to create that query and to think of the logic of the code surrounding it, and then when i got the sql to work in phpmyadmin i was getting pleased only for the error and then i got deflated! Quote Link to comment Share on other sites More sharing options...
magic2goodil Posted February 25, 2007 Share Posted February 25, 2007 Ignore my first code, it's junk. Here's the better working one that has been tested some: $pcodesearch=mysql_query('SELECT `postalcode` FROM `pcode` LIMIT 0, 30 ')or die(mysql_error()); $myArray = array(count(mysql_fetch_assoc($pcodesearch))); $i = 0; while($data = mysql_fetch_assoc($pcodesearch)) { foreach($data as $row) { if(preg_match('/^[A-Z]{1}[0-9]{1,2}/', $row)) { $myArray[$i] = $row; $i++; } else { $i++; } } } print_r($myArray); Quote Link to comment Share on other sites More sharing options...
btherl Posted February 25, 2007 Share Posted February 25, 2007 Hi, The problem is there is an extra space in your code. Possibly it was introduced during cut and paste. The correct code is: $pcodesearch=mysql_query('SELECT `postalcode` FROM `pcode` WHERE `postalcode` REGEXP \'^[a-z]{1}[0-9]{1,2}\' LIMIT 0, 30 ')or die(mysql_error()); Notice the space between the first \ and ' is gone. Quote Link to comment Share on other sites More sharing options...
phpnoob80 Posted February 25, 2007 Author Share Posted February 25, 2007 Hi, The problem is there is an extra space in your code. Possibly it was introduced during cut and paste. The correct code is: oh! sorry i should have picked that up. thank you very much the both of you! jj Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.