Jump to content

php xml and Mysql


webweever

Recommended Posts

I'm using this file http://www.racinnation.com/sandbox/app/xml.txt to pull data from a mysql db and create this file http://www.racinnation.com/sandbox/app/results.xml.

 

I get this error on the results.xml file

Whitespace is not allowed at this location. Error processing resource 'http://www.racinnation.com/sandbox/app/results.xml'....

<?xml version="1.0" encoding="ISO-8859-1" ?><results><item><name>Daytona International Speedway</...

 

I've googled this whitespace error and can't figure out what I'm doing wrong.

 

Anyone have any ideas?

Link to comment
https://forums.phpfreaks.com/topic/125603-php-xml-and-mysql/
Share on other sites

never mind

noticed the link to the code. Here's a re-worked version:

//--------------------------------------------------------------------------
// This is where you specify your Database connection stuff
<?php
include_once("includes/db.connect.inc.php");
// mysql_connect -- Open a connection to a MySQL Server /  die -- Alias of exit()
//
//--------------------------------------------------------------------------
$db_name = "xxxxxx"; //This is your database Name
$link = mysql_connect("$host", "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 = 'markers';

$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\" ?>\r\n";
  $_xml .="<results>\r\n";

//--------------------------------------------------------------------------
// 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 .="\t<item>\r\n";
    $_xml .="\t\t<id>" . $row[id] . "</id>\r\n";
    $_xml .="\t\t<name>" . $row[name] . "</name>\r\n";
    $_xml .="\t\t<address>" . $row[address] . "</address>\r\n";
    $_xml .="\t\t<lat>" . $row[lat] . "</lat>\r\n";
    $_xml .="\t\t<lng>" . $row[lng] . "</lng>\r\n";
    $_xml .="\t\t<type>" . $row[type] . "</type>\r\n";

    //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 .="\t\t<website>http://" . $row[website] . "</website>\r\n";
    } else {
      $_xml .="\t\t<website>http://www.racinnation.com/sandbox/app/</website>\r\n";
    }

    $_xml .="\t\t<deleted>" . $row[deleted] . "</deleted>\r\n";
    $_xml .="\t</item>\r\n";
  }

  $_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";
}

?>  

Link to comment
https://forums.phpfreaks.com/topic/125603-php-xml-and-mysql/#findComment-649458
Share on other sites

forgot the "&" fix

//--------------------------------------------------------------------------
// This is where you specify your Database connection stuff
<?php
include_once("includes/db.connect.inc.php");
// mysql_connect -- Open a connection to a MySQL Server /  die -- Alias of exit()
//
//--------------------------------------------------------------------------
$db_name = "xxxxxx"; //This is your database Name
$link = mysql_connect("$host", "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 = 'markers';

$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\" ?>\r\n";
  $_xml .="<results>\r\n";

//--------------------------------------------------------------------------
// 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.
  	$id = $row['id'];
  	$name = $row['name'];
  	$address = str_replace("&", "and", $row['address']);
  	$lat = $row['lat'];
  	$long = $row['lng'];
  	$type = $row['type'];
    $_xml .="\t<item>\r\n";
    $_xml .="\t\t<id>$id</id>\r\n";
    $_xml .="\t\t<name>$name</name>\r\n";
    $_xml .="\t\t<address>$address</address>\r\n";
    $_xml .="\t\t<lat>$lat</lat>\r\n";
    $_xml .="\t\t<lng>$long</lng>\r\n";
    $_xml .="\t\t<type>$type</type>\r\n";

    //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 .="\t\t<website>http://" . $row[website] . "</website>\r\n";
    } else {
      $_xml .="\t\t<website>http://www.racinnation.com/sandbox/app/</website>\r\n";
    }

    $_xml .="\t\t<deleted>" . $row[deleted] . "</deleted>\r\n";
    $_xml .="\t</item>\r\n";
  }

  $_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";
}

?>  

Link to comment
https://forums.phpfreaks.com/topic/125603-php-xml-and-mysql/#findComment-649463
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.