Connecting Tech Pros Worldwide Help | Site Map

Creating SQL queries through PHP reading in XML file

Familiar Sight
 
Join Date: Sep 2008
Posts: 252
#1: 3 Weeks Ago
I have the below code to read in the XML file and to print the query to screen to see what its generating. I have the XML file below that. I'm having problems checking for the for attributes like primary or autonumber, im unable to use if statements and I can't check if there is more then one field to create so that it adds in a colon. Any help much appreciated.

This is what the below ouputs:
Quote:
CREATE TABLE test_table_1 ( id INT(3) )
CREATE TABLE test_table_2 ( id IN(10) test VARCHAR(100) test_2 VARCHAR(15) )
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $file = "queries.xml";
  3. $xml = simplexml_load_file($file) or die ("Unable to load XML file!");
  4.  
  5. foreach ($xml->tables->table as $table) {
  6.     $query = 'CREATE TABLE '.$table['name'].' ( ';
  7.  
  8.     foreach ($table->column as $column) {
  9.         $query .= $column['name'].' '.$column['type'].'('.$column['size'].') ';
  10.     }
  11.     $query .= ' )';
  12.  
  13.     print $query.'<br/>';
  14. }
  15. ?> 
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <structure>
  3.     <tables>
  4.         <table name="test_table_1">
  5.             <column name="id" type="INT" size="3" primary="true" autonumber="true" />
  6.         </table>
  7.         <table name="test_table_2">
  8.             <column name="id" type="IN" size="10" primary="true" autonumber="true" />
  9.             <column name="test" type="VARCHAR" size="100" />
  10.             <column name="test_2" type="VARCHAR" size="15" />
  11.         </table>
  12.     </tables>
  13. </structure>
Familiar Sight
 
Join Date: Sep 2008
Posts: 252
#2: 3 Weeks Ago

re: Creating SQL queries through PHP reading in XML file


I've gotten a step further with the below code displaying the below queries, my only problem now is how to add the commas to seperate the different field data?
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $file = "queries.xml";
  3. $xml = simplexml_load_file($file) or die ("Unable to load XML file!");
  4.  
  5. foreach ($xml->tables->table as $table) {
  6.     $query = 'CREATE TABLE '.$table['name'].' ( ';
  7.  
  8.     foreach ($table->column as $column) {
  9.         $query .= $column['name'].' '.$column['type'].'('.$column['size'].') ';
  10.         if($column['autonumber']=='true')
  11.             $query .= 'NOT NULL AUTO_INCREMENT ';
  12.         if($column['primary']=='true')
  13.             $query .= 'PRIMARY KEY ';
  14.     }
  15.     $query .= ' )';
  16.  
  17.     print $query.'<br/>';
  18. }
  19. ?>
Quote:
CREATE TABLE test_table_1 ( id INT(3) NOT NULL AUTO_INCREMENT PRIMARY KEY )
CREATE TABLE test_table_2 ( id INT(10) NOT NULL AUTO_INCREMENT PRIMARY KEY test VARCHAR(100) test_2 VARCHAR(15) )
TheServant's Avatar
Expert
 
Join Date: Feb 2008
Location: Australia
Posts: 913
#3: 3 Weeks Ago

re: Creating SQL queries through PHP reading in XML file


In your column foreach loop simply add a comma to the end of every loop, and then once you loop is finished you can remove the last comma added by using substr.
Familiar Sight
 
Join Date: Sep 2008
Posts: 252
#4: 3 Weeks Ago

re: Creating SQL queries through PHP reading in XML file


Quote:

Originally Posted by TheServant View Post

In your column foreach loop simply add a comma to the end of every loop, and then once you loop is finished you can remove the last comma added by using substr.

Got it sorted using a str_replace, thanks.
TheServant's Avatar
Expert
 
Join Date: Feb 2008
Location: Australia
Posts: 913
#5: 3 Weeks Ago

re: Creating SQL queries through PHP reading in XML file


No worries. .
Reply