Jump to content

php and xml


woodsonoversoul

Recommended Posts

Hi all. I'm trying to learn to manipulate xml structure to use php to work with ajax. I'm working with a simple script which retrieves a certain set of data and then returns it as an xml file. I'm pretty new to this so the error(s) could lie anywhere.

 

Here's my error message:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

 

and here's my code:

<?php
header('Content-Type: text/xml');

//connect with database
$con = mysql_connect("localhost","***","");
if(!$con){
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("***", $con);
//end connecting to database

//get a listing of artist
$artist = "";
$artist_array = array();
$result = mysql_query("SELECT * FROM songs WHERE artist LIKE $artist");
//error check - see if this is safe to remove later
if(!$result){
  die(mysql_error());
  }
  
//check if any results were returned
if(mysql_num_rows($result) > 0){
  //if so, display year
while($row = mysql_fetch_array($result)){
          		//check to see if this $rows date is different than the stored $date
          		if($row['artist'] != $artist){
          			//if yes them display that songs date
          			$artist_array[] =  $row['artist'];

			}//end if
			// display songs
          		//echo "<li class=\"song\" onmouseover=\"this.style.backgroundColor='yellow';\" onmouseout=\"this.style.backgroundColor='lightblue';\">" . $row['name'] . "</li>";
          		//reset $date to current song date
          		$artist = $row['artist'];
          		
		}//end while
	}//end if
mysql_close($con);//close mysql connection

//DOM manipulation
$dom = new DOMDocument();
$response = $dom->createElement('response');
$dom->appendChild($response);

for($i = 0; $i < count($artist_array); $i++){
$artistElement = $dom->createElement('artist');
$artistText = $dom->createTextNode($artist_array[$i]);
$artistElement->appendChild($artistText);

$response->appendChild($artistElement);
}
$xmlString = $dom->saveXML(); 
echo $xmlString;
?>

 

I'm assuming it's an error with the header. All help is much appreciated  :)

 

NOTE: please ignore the comments, this was mostly copied from another script of mine

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

they problem is in your SQL query. you have this block of code:

$artist = "";
$artist_array = array();
$result = mysql_query("SELECT * FROM songs WHERE artist LIKE $artist");

since $artist is empty, the SQL from that will look like:

"SELECT * FROM songs WHERE artist LIKE "

which is not valid. what are you trying to accomplish here? where the artist field is empty?

 

Link to comment
https://forums.phpfreaks.com/topic/143950-php-and-xml/#findComment-755386
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.