REGEX trouble

  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8416
  • Loc: USA

Post 3+ Months Ago

I'm trying to create some regex to retrieve some variables out of an SQL query... this is what I have so far

Code: [ Select ]
#(SELECT)(.*?)FROM(.*?)WHERE(.*?)ORDER BY(.*?)(ASC|DESC)#


The problem with that is that it doesn't give any results if I don't have ORDER BY or WHERE or whatever... I understand why I just don't know how to fix that... I would want it to give me an array of anything it could find.

What I'm trying to do is to explode an SQL string into an associative array... example below...

Code: [ Select ]
$sql = "SELECT email FROM users WHERE username = 'Billy Bob Joes'";


Once passed through the function, that would transform into...

Code: [ Select ]
array('SELECT' => 'email',
'FROM' => 'users',
'WHERE' => 'username = \'Billy Bob Joes\''
);
  1. array('SELECT' => 'email',
  2. 'FROM' => 'users',
  3. 'WHERE' => 'username = \'Billy Bob Joes\''
  4. );


Or, if the SQL string has the ORDER BY ID ASC. The array would look like

Code: [ Select ]
array('SELECT' => 'email',
'FROM' => 'users',
'WHERE' => 'username = \'Billy Bob Joes\'',
'ORDER BY' => 'ID ASC'
);
  1. array('SELECT' => 'email',
  2. 'FROM' => 'users',
  3. 'WHERE' => 'username = \'Billy Bob Joes\'',
  4. 'ORDER BY' => 'ID ASC'
  5. );


And so on... the reason I posted what I want is maybe there is a better way to do this, or maybe there is a function already that does this.

And explode(" ", $sql); doesn't really work for obvious reasons :lol:
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8416
  • Loc: USA

Post 3+ Months Ago

Note: This would be for trivial SQL queries... nothing fancy with UNION or anything like it. Simple SELECT (something) FROM (somewhere) where (Something is that thing) ORDER BY (this thing) ASC -or- DESC
  • alex89
  • Bronze Member
  • Bronze Member
  • User avatar
  • Posts: 239
  • Loc: Western Australia

Post 3+ Months Ago

I might do something like:

Code: [ Select ]
#get variables from drop down boxes etc?
$select = "email";
$from = "users";
$where = "username = \'billybob\'";
$order = "ID ASC";

#if not blank, add to query
if (!empty($select)){$query = "SELECT ".$select;}
if (!empty($from)){$query = " FROM ".$query.$from;}
if (!empty($where)){$query = " WHERE ".$query.$where;}
if (!empty($order)){$query = " ODER BY ".$query.$order;}

echo $query;
  1. #get variables from drop down boxes etc?
  2. $select = "email";
  3. $from = "users";
  4. $where = "username = \'billybob\'";
  5. $order = "ID ASC";
  6. #if not blank, add to query
  7. if (!empty($select)){$query = "SELECT ".$select;}
  8. if (!empty($from)){$query = " FROM ".$query.$from;}
  9. if (!empty($where)){$query = " WHERE ".$query.$where;}
  10. if (!empty($order)){$query = " ODER BY ".$query.$order;}
  11. echo $query;


That seems the simplest way to do it - arrays might be complicating things a little.
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8416
  • Loc: USA

Post 3+ Months Ago

I don't need that... I want a basic SELECT query that is held in a string to be exploded into that associative array.
  • alex89
  • Bronze Member
  • Bronze Member
  • User avatar
  • Posts: 239
  • Loc: Western Australia

Post 3+ Months Ago

Oh sorry, I misunderstood the question. :) I'll have a better look later, got to go out now.

Post Information

  • Total Posts in this topic: 5 posts
  • Users browsing this forum: No registered users and 60 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.