Jump to content

[SOLVED] Help with a PHP script including MySQL REGEXP


phpnoob80

Recommended Posts

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

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.