title: how to extract table names from mysql query with php?
body: <p>I wanna detect all tables in an mysql query with php. I saw mysql_field_table() function that is work for normal queries. but when you use alternative name for a table (<strong>table_name AS new_name</strong>) it return alternative name not the real name, how ever I fix it by a regex. but now my problem is now with advanced queries like this:</p>
  
  <pre><code>SELECT mail_id, mail_date, mail_from, mail_to, mail_subject, (
              (
              SELECT COUNT(*)
              FROM `sys_messagecenter_qmails`
              WHERE qmail_mail_id = mail_id
              ) + (
              SELECT COUNT(*)
              FROM `sys_messagecenter_rels`
              WHERE rel_mail_id = mail_id
              )
              ) AS email_total, (
              SELECT COUNT(*)
              FROM `sys_messagecenter_rels`
              WHERE rel_mail_id = mail_id
              ) AS email_sent, (
              SELECT COUNT(*)
              FROM `sys_messagecenter_rels`
              INNER JOIN `sys_email_receives` ON receive_reply_to = rel_sent_id
              WHERE rel_mail_id = mail_id
              ) AS email_reply FROM `sys_messagecenter_emails` WHERE mail_draft='No' 
  ORDER BY mail_id ASC LIMIT 0,10 
  </code></pre>
  
  <p>now I think I need some regex for detect all real table names in a query. or any other perfect solution. how can I do this?</p>