Zend - The PHP Company




XML

Add Code


Generate XML from PHP and mySQL  

Type: code fragment
Added by: iamtgo3
Entered: 14/05/2003
Last modified: 05/12/2002
Rating: - (fewer than 3 votes)
Views: 19607
This basic example will allow you to generate and XML file using PHP and data from your mySQL database. It also has the ability to save the XML Doc if you read through the code and uncomment the three lines of code that do it.


//--------------------------------------------------------------------------
// This is where you specify your Database connection stuff
//
// mysql_connect -- Open a connection to a MySQL Server /  die -- Alias of exit()
//
//--------------------------------------------------------------------------
$db_name = "mycompany"; //This is your database Name
$link = mysql_connect("localhost", "UserName", "PassWord") or die("Could not connect to server!");
//This is your table name. This is a one table config to do more table you will need to rework the code.
$table_name = 'userinfo';

$select_db = mysql_select_db($db_name, $link); // mysql_select_db -- Select a MySQL database
$query = "SELECT * FROM " . $table_name;
$result = mysql_query($query, $link) or die("Could not complete database query"); //mysql_query -- Send a MySQL query
$num = mysql_num_rows($result); //mysql_num_rows -- Get number of rows in result

if ($num != 0) {

//--------------------------------------------------------------------------
// If you would like to save a copy of the XML Doc being created uncomment this
// line and the two lines at the bottom containing fwrite and fclose.
//
//
//--------------------------------------------------------------------------
  //$file= fopen("results.xml" , "w"); //fopen -- Opens file or URL

//--------------------------------------------------------------------------
// XML Header Tag Goes Here
//
//
//
//--------------------------------------------------------------------------
  $_xml ="<?xml version="1.0" encoding="UTF-8" ?>rn";
  $_xml .="<results>rn";

//--------------------------------------------------------------------------
// This while loop loops throught the data found from the above query and
// generates the XML Doc.
//
//
//--------------------------------------------------------------------------
  while ($row = mysql_fetch_array($result)) { //mysql_fetch_array --  Fetch a result row as an associative array, a numeric array, or both.
    $_xml .=" <item>rn";
    $_xml .="   <recordnumber>" . $row[RecordID] . "</recordnumber>rn";
    $_xml .="   <username>" . $row[username] . "</username>rn";
    $_xml .="   <address>" . $row[address] . "</address>rn";
    $_xml .="   <city>" . $row[city] . "</city>rn";
    $_xml .="   <state>" . $row[state] . "</state>rn";
    $_xml .="   <zip>" . $row[zip] . "</zip>rn";
    $_xml .="   <phonenumber>" . $row[phonenumber] . "</phonenumber>rn";
    $_xml .="   <email>" . $row[email] . "</email>rn";

    //You can use if statements to check if the returned value is null.
    //If it is just place a default value in the tags like below.
    if ($row[website]) {
      $_xml .="   <website>http://" . $row[website] . "</website>rn";
    } else {
      $_xml .="   <website>http://www.ipdg3.com</website>rn";
    }

    $_xml .="   <deleted>" . $row[deleted] . "</deleted>rn";
    $_xml .=" </item>rn";
  }

  $_xml .="</results>";

//--------------------------------------------------------------------------
// If you would like to save a copy of the XML Doc being created uncomment this
// line and the two lines at the bottom containing fwrite and fclose.
//
//
//--------------------------------------------------------------------------
  //fwrite($file, $_xml); //fwrite -- Binary-safe file write
  //fclose($file); //fclose -- Closes an open file pointer

//--------------------------------------------------------------------------
// This will echo the XML file out to the screen so you can view it.
//
//
//
//--------------------------------------------------------------------------
  echo $_xml;

} else {
  echo "No Records found";
}

?>


Usage Example


See the example


Rate This Script





Search



This Category All Categories