Current location: Hot Scripts Forums » Programming Languages » PHP » xml to mysql using php - urgent


xml to mysql using php - urgent

Reply
  #1 (permalink)  
Old 12-20-06, 02:45 PM
mianriz's Avatar
mianriz mianriz is offline
New Member
 
Join Date: Jun 2005
Location: Pakistan
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
xml to mysql using php - urgent

Issue is have a "xml" file and I have created mysql database using php

and fatch data from "xml" file. MY table and all data placed

accordingly but I am facing problem with one atribute.

PHP Code:

<ToLet Version="1.0" Agent="abc" Password="123" Date="20040813">

 <
Property>
  <
Refnumber>1.00</Refnumber
  <
Number>1a</Number
  <
Street>Madina Town</Street
  <
Address3>St Johns Wood</Address3
  <
Address4>London</Address4
  <
Postcode>NW6 1HY</Postcode
  <
Area>St Johns Wood</Area
  <
Type>Conversion</Type
  <
Beds>2.00</Beds
  <
Receps>1.00</Receps
  <
Garage>False</Garage
  <
Parking>False</Parking
  <
Garden>False</Garden
  <
Balcony>True</Balcony
  <
Floor>G</Floor
  <
Available>29/02/2004</Available
  <
Rent Per="Week">200.00</Rent
  <
Description>Delightful 2 bed flat in sought after location close to 

all amenities
</Description
  <
Comments>Delightful 2 bed flat in sought after location close to all 

amenities
</Comments
  <
Photo1>abc_1_1.jpg</Photo1
  <
Photo2>abc_1_2.jpg</Photo2
  <
Photo3>abc_1_3.jpg</Photo3
  <
Photo4>abc_1_4.jpg</Photo4
  <
Photo5>abc_1_5.jpg</Photo5
  <
Photo6>abc_1_6.jpg</Photo6
  <
Photo7>abc_1_7.jpg</Photo7
  <
Photo8>abc_1_8.jpg</Photo8
  <
Photo9>abc_1_9.jpg</Photo9
  <
Map /> 
  <
Movie /> 
  <
Url>http://www.rman.co.uk</Url> 
 
</Property>
 <
Property>
  <
Refnumber>2.00</Refnumber
  <
Number>1b</Number
  <
Street>Gulburg</Street
  <
Address3>St Johns Wood</Address3
  <
Address4>London</Address4
  <
Postcode>NW8</Postcode
  <
Area>St Johns Wood</Area
  <
Type>Conversion</Type
  <
Beds>2.00</Beds
  <
Receps>1.00</Receps
  <
Garage>False</Garage
  <
Parking>False</Parking
  <
Garden>False</Garden
  <
Balcony>False</Balcony
  <
Floor>1</Floor
  <
Available>29/09/2004</Available
  <
Rent Per="Week">250.00</Rent
  <
Description>Delightful 3 bed flat in new conversionClose to all 

amenities
</Description
  <
Comments>Delightful 3 bed flat in new conversionClose to all 

amenities
</Comments
  <
Photo1>abc_2_1.jpg</Photo1
  <
Photo2>abc_2_2.jpg</Photo2
  <
Photo3>abc_2_3.jpg</Photo3
  <
Photo4>abc_2_4.jpg</Photo4
  <
Photo5>abc_2_5.jpg</Photo5
  <
Photo6>abc_2_6.jpg</Photo6
  <
Photo7>abc_2_7.jpg</Photo7
  <
Photo8>abc_2_8.jpg</Photo8
  <
Photo9>abc_2_9.jpg</Photo9
  <
Map /> 
  <
Movie /> 
  <
Url /> 
 </
Property>
</
ToLet
In this file all atribute readed because every thing same but one

atribute <Rent Per="Week">200.00</Rent> in different method and

<Rent Per="Week"> contian two conditions one is Rent per Week and other

will Rent per month.
<Rent Per="Week">200.00</Rent> in this lines we fatch 200.00 in our

databse but we also want to fatch "week" in our database.

We have write a php file to fatch data from XML to mySQL wich working

fine instead of <Rent Per="Week"> issue.

My PHP file code is as:

PHP Code:

<?php

define
("DB_HOST","localhost",TRUE); // database server name
define("DB_USER","root",TRUE); // database user
define("DB_PASS","1234",TRUE); // database user's password
define("DB_NAME","xml",TRUE); // database name

$TAG "EXTRA";
$id 0;
//Initialize the XML parser
$parser=xml_parser_create();

//Function to use at the start of an element
function start($parser,$element_name,$element_attrs)
{
    global 
$TAG
    
    switch(
$element_name)
    {
        case 
"PROPERTY":
        echo 
"<br />[Property]";
        
$TAG "PROPERTY";
        break;        
        case 
"REFNUMBER":
        
$TAG "REFNUMBER";
        break;        
        case 
"NUMBER":
        
$TAG "NUMBER";
        break;        
        case 
"STREET":
        
$TAG "STREET";
        break;        
        case 
"ADDRESS3":
        
$TAG "ADDRESS3";
        break;        
        case 
"ADDRESS4":
        
$TAG "ADDRESS4";
        break;        
        case 
"POSTCODE":
        
$TAG "POSTCODE";
        break;        
        case 
"AREA":
        
$TAG "AREA";
        break;        
        case 
"TYPE":
        
$TAG "TYPE";
        break;        
        case 
"BEDS":
        
$TAG "BEDS";
        break;        
        case 
"RECEPS":
        
$TAG "RECEPS";
        break;        
        case 
"GARAGE":
        
$TAG "GARAGE";
        break;        
        case 
"PARKING":
        
$TAG "PARKING";
        break;        
        case 
"GARDEN":
        
$TAG "GARDEN";
        break;        
        case 
"BALCONY":
        
$TAG "BALCONY";
        break;
        case 
"FLOOR":
        
$TAG "FLOOR";
        break;        
        case 
"AVAILABLE":
        
$TAG "AVAILABLE";
        break;
        case 
"RENT":
        
$TAG "RENT";
        break;        
        case 
"DESCRIPTION":
        
$TAG "DESCRIPTION";
        break;
        case 
"COMMENTS":
        
$TAG "COMMENTS";
        break;        
        case 
"PHOTO1":
        
$TAG "PHOTO1";
        break;
        case 
"PHOTO2":
        
$TAG "PHOTO2";
        break;        
        case 
"PHOTO3":
        
$TAG "PHOTO3";
        break;
        case 
"PHOTO4":
        
$TAG "PHOTO4";
        break;        
        case 
"PHOTO5":
        
$TAG "PHOTO5";
        break;
        case 
"PHOTO6":
        
$TAG "PHOTO6";
        break;        
        case 
"PHOTO7":
        
$TAG "PHOTO7";
        break;
        case 
"PHOTO8":
        
$TAG "PHOTO8";
        break;        
        case 
"PHOTO9":
        
$TAG "PHOTO9";
        break;
        case 
"MAP":
        
$TAG "MAP";
        break;        
        case 
"MOVIE":
        
$TAG "MOVIE";
        break;
        case 
"URL":
        
$TAG "URL";
        break;        
        
        default:
        
$TAG "EXTRA";
    }
}

//Function to use at the end of an element
function stop($parser,$element_name)
 {
      echo 
"";
  
 }

//Function to use when finding character data
function char($parser,$data)
{
    global 
$TAG
    global 
$id;

    require_once(
'mysql.class.php');

    if( !isset(
$db_obj) ) {
        
$db_obj = new db
    }
        
    
$db_conect $db_obj->connect();

    if( 
$TAG != "EXTRA") {
        
        
        echo 
"<br />$data <font color=#006600 

size=2>[OK]</font>"
;
        
        
        if(
$TAG == "PROPERTY") {
            
            
$sql $db_obj->query("select max(id) as id 

from property "
,$db_conect);
            if( 
$rs $db_obj->fetch_array($sql) ) {
                
$id $rs['id'];
            }
            
$id $id 1;
        
            
$db_obj->query("insert into property (id) 

values('
$id') ",$db_conect);
            
        }
        else if( 
$TAG == "REFNUMBER" || $TAG == "NUMBER" || 

$TAG == "STREET" || $TAG == "ADDRESS3" || $TAG == "ADDRESS4" || 
        
$TAG == "POSTCODE" || $TAG == "AREA" || $TAG == "TYPE" 

|| $TAG == "BEDS" || $TAG == "RECEPS" || $TAG == "GARAGE" || 
        
$TAG == "PARKING" || $TAG == "GARDEN" || $TAG == 

"BALCONY" || $TAG == "FLOOR" || $TAG == "AVAILABLE" || $TAG == "RENT"
        
|| $TAG == "DESCRIPTION" || $TAG == "COMMENTS" || $TAG 

== "PHOTO1" || $TAG == "PHOTO2" || $TAG == "PHOTO3" || 
        
$TAG == "PHOTO4" || $TAG == "PHOTO5" || $TAG == 

"PHOTO6" || $TAG == "PHOTO7" || $TAG == "PHOTO8" || $TAG == "PHOTO9" 
        
|| $TAG == "MAP" || $TAG == "MOVIE" || $TAG == "URL" 

{
        
            
$db_obj->query("update property set 

$TAG='$data' where id='$id' ",$db_conect);
        }
        
        
$TAG "EXTRA";
        
    }
    
}


//Specify element handler
xml_set_element_handler($parser,"start","stop");

//Specify data handler
xml_set_character_data_handler($parser,"char");

//Open XML file
$fp=fopen("rentman.xml","r");

//Read data
while ($data=fread($fp,4096))
  {
  
xml_parse($parser,$data,feof($fp)) or 
  die (
sprintf("XML Error: %s at line %d"
  
xml_error_string(xml_get_error_code($parser)),
  
xml_get_current_line_number($parser)));
  }

//Free the XML parser
xml_parser_free($parser);

?>
I also use a class for this purpose as below:

PHP Code:

<?php


class db {


    function 
connect() {
        
$die false;
        
$link = @mysql_connect(DB_HOSTDB_USER,DB_PASS) or 

(
$die true);
                
        if(
$die){
                    
            echo 
'<h3>Database connection error!!!</h3>';
            
            echo 
'A connection to the Database could not be 

established.<br />'
;
            echo 
'Please check your username, password, 

database name and host.<br />'
;
            echo 
'Also make sure <i>mysql.class.php</i> is 

rightly configured!<br /><br />'
;
            
            exit();
        }
        
        
mysql_select_db(DB_NAME);
        
        return 
$link;
    }

    
// Returns an array that corresponds to the fetched row
    
function fetch_array($query) {
        
$query mysql_fetch_array($query,MYSQL_ASSOC);
        return 
$query;
    } 
    
// To Execute Sql Query
    
function query($sql,$res) {
        
$query mysql_query($sql) or die(mysql_error());
        return 
$query;
    }
    
// Retrieves the number of rows from a result set
    
function num_rows($query) {
        
$query mysql_num_rows($query);
        return 
$query;
    }
    
// Get the number of affected rows by the last INSERT, UPDATE 

or DELETE query
    
function affected_rows($query) {
        
$query =   mysql_affected_rows();
        return 
$query;
    }
    
}
?>
Now in this situation I also want to fatch data of this attribute <Rent

Per="Week"> in my databse. In thi I want to store "week" in my databse.

For this purpose please write any quote within my quote and let me know

changes... I am waiting your reply
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 12-21-06, 04:51 PM
Shaffer Shaffer is offline
Wannabe Coder
 
Join Date: Sep 2006
Location: Israel
Posts: 149
Thanks: 0
Thanked 0 Times in 0 Posts
Well, first-off:
The second code you've entered is missing the variavble: $db_connect;
Not only that, when you call it, you used $db_conect;


Shaffer.
__________________
Hello from Israel!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP and MySQL ? rob2132 Hot Scripts Forum Questions, Suggestions and Feedback 4 08-29-08 03:22 AM
Php Mysql Bug??? tranquilraven PHP 4 03-01-06 04:06 AM
URGENT: mySQL and PHP Expert Wanted MZF Job Offers & Assistance 4 12-21-05 02:58 PM
PHP multi-dimensional array sorting issue aqw PHP 2 06-25-05 12:09 AM
Complex mysql sorting pb (Get cat_list from cids &pcids with 1 query, willing to pay) aqw PHP 1 06-23-05 08:02 PM


All times are GMT -5. The time now is 06:45 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.