woodsonoversoul Posted February 5, 2009 Share Posted February 5, 2009 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 More sharing options...
rhodesa Posted February 5, 2009 Share Posted February 5, 2009 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 More sharing options...
woodsonoversoul Posted February 5, 2009 Author Share Posted February 5, 2009 You're absolutely right. I saw it and fixed it. Thanks Link to comment https://forums.phpfreaks.com/topic/143950-php-and-xml/#findComment-755393 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.