I'm COMPLETELY new to PHP. I need it only for a short project for work to import a CSV into a MYSQL database, and then query the database via the Google Maps API. If you can, I'd appreciate baby-steps / resources.
I'm trying to connect to a database containing names, longitudes, and latitudes (among other things), execute a SELECT * query on the markers table, and iterate through the results. For each row in the table (each location), I need to create a new XML node with the row attributes as XML attributes, and append it to the parent node. Then dump the XML to the screen.
I'm using this code:
<?php
require("phpsqlajax_dbinfo.php");
// Start XML file, create parent node
$dom = new DOMDocument("1.0");
$node = $dom->createElement("markers");
$parnode = $dom->appendChild($node);
// Opens a connection to a MySQL server
$connection=mysql_connect (localhost, $username, $password);
if (!$connection) { die('Not connected : ' . mysql_error());}
// Set the active MySQL database
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
die ('Can\'t use db : ' . mysql_error());
}
// Select all the rows in the markers table
$query = "SELECT * FROM markers WHERE 1";
$result = mysql_query($query);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
header("Content-type: text/xml");
// Iterate through the rows, adding XML nodes for each
while ($row = @mysql_fetchAssoc($result)){
// ADD TO XML DOCUMENT NODE
$node = $dom->createElement("marker");
$newnode = $parnode->appendChild($node);
$newnode->setAttribute("name", $row['name']);
$newnode->setAttribute("city", $row['city']);
$newnode->setAttribute("country", $row['country']);
$newnode->setAttribute("lat", $row['lat']);
$newnode->setAttribute("lng", $row['lng']);
$newnode->setAttribute("skill_1", $row['skill_1']);
$newnode->setAttribute("skill_2", $row['skill_2']);
$newnode->setAttribute("skill_3", $row['skill_3']);
$newnode->setAttribute("interest_1", $row['interest_1']);
$newnode->setAttribute("interest_2", $row['interest_2']);
$newnode->setAttribute("interest_3", $row['interest_3']);
}
echo $dom->saveXML();
?>
I've tried to do elementary debugging, and I can establish that 1) the program is correctly connecting to the database; 2) no error log is showing up; 3) the error appears to start right after the comment " // Select all the rows in the markers table."
XML data is supposed to show up, but it doesn't, and I've been pulling my hair out for two hours. Any ideas?
==EDIT== I didn't realize that '@' silenced error messages. Upon removing it, this error log shows up on my website. Any ideas from this?
[11-Jul-2013 13:08:00] PHP Warning: Cannot modify header information - headers already sent by (output started at /home5/dreamio2/public_html/admin/phpsqlajax_dbinfo.php:7) in /home5/dreamio2/public_html/admin/phpsqlajax_genxml.php on line 31
[11-Jul-2013 13:08:00] PHP Warning: DOMElement::setAttribute() [<a href='domelement.setattribute'>domelement.setattribute</a>]: string is not in UTF-8 in /home5/dreamio2/public_html/admin/phpsqlajax_genxml.php on line 40
[11-Jul-2013 13:08:00] PHP Warning: DOMElement::setAttribute() [<a href='domelement.setattribute'>domelement.setattribute</a>]: string is not in UTF-8 in /home5/dreamio2/public_html/admin/phpsqlajax_genxml.php on line 40
[11-Jul-2013 13:08:00] PHP Warning: DOMDocument::saveXML() [<a href='domdocument.savexml'>domdocument.savexml</a>]: output conversion failed due to conv error, bytes 0xE9 0x73 0x20 0x41 in /home5/dreamio2/public_html/admin/phpsqlajax_genxml.php on line 54
==SOLUTION==
After a long discussion with Manoj, it turns out I had two errors: 1) I needed to UTF_8 encode all my variables (see his answer); 2) my php file that contained my username and password has TRAILING WHITESPACE after the close statement. Thanks, Manoj!