RE: [PHP] Re: Extract column names from a (my)SQL query

2009-08-21 Thread Daevid Vincent
 -Original Message-
 From: Nisse Engström [mailto:news.nospam.0ixbt...@luden.se] 

 If you're using MySQL, you can try mysql_field_name()
 and see if it gets you anywhere. I don't think it works
 on empty results though.


FYI. It will.


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Re: Extract column names from a (my)SQL query

2009-08-21 Thread דניאל דנון
You all misunderstood my question, please read my replies above...

I'm looking to extract it from a string - *I'm not executing the queries, I
only get them as a string*

And to the topic:

Since everything I found was very complicated to parse, I've crafted my own
preg pattern,

/^(\*|[a-z_, \(\)0-9]+)[\s]+FROM[\s]+([a-z_\.
]+)\s+(WHERE[\s]+(.+)|)\s*(|ORDER BY ([a-z0-9,
])(|\s(DESC|ASC)))\s*(|LIMIT\s+([0-9]+)\s*,\s*([0-9]+))\s*$/Ui

(Assuming this is a SELECT string, and the subject is something similar to:
* FROM table WHERE field2='field3' LIMIT 0,10

(Yes, no SELECT - I've got another function to determine whether its select,
insert, update, delete from - etc, and it returns to query without the name
of the command - SELECT field FROM table = field FROM table)

(Without using joins or things like that)

But I don't have much experience crafting patterns like that - or working
with them so I'd be glad if  you think of a better way of doing it,

and problem is since I'm using sub-brackets, its hard to process it
since if there is WHERE and a LIMIT, then LIMIT (...) will be on $result[$n]
(for example),
But if there is no WHERE, then LIMIT (...) will be on $result[$n - 2];

How should I overcome this problem?


2009/8/21 Daevid Vincent dae...@daevid.com

  -Original Message-
  From: Nisse Engström [mailto:news.nospam.0ixbt...@luden.se]
 
  If you're using MySQL, you can try mysql_field_name()
  and see if it gets you anywhere. I don't think it works
  on empty results though.


 FYI. It will.


 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php




-- 
Use ROT26 for best security


Re: [PHP] Re: Extract column names from a (my)SQL query

2009-08-21 Thread דניאל דנון
Update: I've changed it into

/^(\*|[a-z_,
\(\)0-9]+)[\s]+FROM[\s]+([a-z_\.]+)(\s+)?(WHERE[\s]+(.+))?\s*(LIMIT\s+([0-9]+)\s*,\s*([0-9]+))?\s*(ORDER
BY ([a-z0-9, ]+)?(\s*(DESC|ASC)))?$/Ui


Only problem that on:
SELECT * FROM table WHERE field2='field3' ORDER BY id DESC LIMIT 0,10
it outputs

Array
(
[0] = * FROM table WHERE field2='field3' ORDER BY id DESC LIMIT 0,10
[1] = *
[2] = table
[3] =
[4] = WHERE field2='field3' ORDER BY id DESC
[5] = field2='field3' ORDER BY id DESC
[6] = LIMIT 0,10
[7] = 0
[8] = 10
)



On Fri, Aug 21, 2009 at 9:38 PM, דניאל דנון danondan...@gmail.com wrote:

 You all misunderstood my question, please read my replies above...

 I'm looking to extract it from a string - *I'm not executing the queries,
 I only get them as a string*

 And to the topic:

 Since everything I found was very complicated to parse, I've crafted my own
 preg pattern,

 /^(\*|[a-z_, \(\)0-9]+)[\s]+FROM[\s]+([a-z_\.
 ]+)\s+(WHERE[\s]+(.+)|)\s*(|ORDER BY ([a-z0-9,
 ])(|\s(DESC|ASC)))\s*(|LIMIT\s+([0-9]+)\s*,\s*([0-9]+))\s*$/Ui

 (Assuming this is a SELECT string, and the subject is something similar to:
 * FROM table WHERE field2='field3' LIMIT 0,10

 (Yes, no SELECT - I've got another function to determine whether its
 select, insert, update, delete from - etc, and it returns to query without
 the name of the command - SELECT field FROM table = field FROM table)

 (Without using joins or things like that)

 But I don't have much experience crafting patterns like that - or working
 with them so I'd be glad if  you think of a better way of doing it,

 and problem is since I'm using sub-brackets, its hard to process it
 since if there is WHERE and a LIMIT, then LIMIT (...) will be on $result[$n]
 (for example),
 But if there is no WHERE, then LIMIT (...) will be on $result[$n - 2];

 How should I overcome this problem?


 2009/8/21 Daevid Vincent dae...@daevid.com

  -Original Message-
  From: Nisse Engström [mailto:news.nospam.0ixbt...@luden.se]
 
  If you're using MySQL, you can try mysql_field_name()
  and see if it gets you anywhere. I don't think it works
  on empty results though.


 FYI. It will.


 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php




 --
 Use ROT26 for best security




-- 
Use ROT26 for best security