Jump to content

PHP XLM/JSON API problem with mySQL connect include


dazzathedrummer

Recommended Posts

Hi,

 

This is an odd one (to me anyway). I've created an API for deploying gig information in either XML or JSON format.

 

It works fine as it is, both formats of output giving me what I want.

 

When I take out the mySQL login information and replace it with "include 'connect_include.php';" (where the file connect_include.php contains the exact login code that I replced).

The JSON output works fine but the XML output doesn't run.

 

What could be causing this??

 

 

JSON API: -http://www.api.the-guards.co.uk/jsontest.php?apikey=123456&format=json

 

XML API: - http://www.api.the-guards.co.uk/jsontest.php?apikey=123456&format=xml

 

 

API code:

<?php
if (($_GET['apikey'])==123456 and isset($_GET['format'])){

//Set variables
$format = strtolower($_GET['format']);

//Connect to the Database
include 'connect_include.php';

//Run our query
$result = mysql_query('
SELECT * 
FROM tg_gig_list 
where
gl_date >= curdate()
and gl_publish = 1
order by gl_date

');

//Preapre our output
if($format == 'json') {

while ($row = mysql_fetch_array($result,MYSQL_ASSOC))
{ 
$array[] = array($row['gl_date']=>($row['gl_venue'].', '.$row['gl_city']));
};


$output = json_encode($array);




//Output the output.
echo $output;
header('Content-type: application/json');

}

elseif($format == 'xml') {
header('Content-type: text/xml');
$output  = "<?xml version=\"1.0\"?>\n";
$output .= "<tggiglist>\n";

for($i = 0 ; $i < mysql_num_rows($result) ; $i++){
$row = mysql_fetch_assoc($result);
$output .= "<giglist> \n";
$output .= "<giglist_date>" . $row['gl_date'] . "</giglist_date> \n";
$output .= "<giglist_venue>" . $row['gl_venue'] . "</giglist_venue> \n";
$output .= "<giglist_city>" . $row['gl_city'] . "</giglist_city> \n";
$output .= "</giglist> \n";
}

$output .= "</tggiglist>";

}


}
else {
die ('no way');
}

//output the output
echo $output;
?>

 

Contents of 'connect_include.php:

 

 <?php 

mysql_connect("database.lcn.com", "dbname", "password") or die(mysql_error()); 
mysql_select_db("db") or die(mysql_error()); 
?> 

.....this is the same code that I replaced.

 

Obviously, the 'connect' works ok as the JSON version runs fine. There's something being added to the scripting when I use the include file that isn't there when the code goes straight in.....I think????

 

 

 

 

Link to comment
Share on other sites

Your connect_include.php file probably contains some character(s) before the <?php tag or after the ?> tag or the file was saved as a UTF-8 encoded file with the BOM (Byte Order Mark) character.

 

Insure there is nothing before or after the <?php ?> tags and that the file is saved as an ANSI/ASCII file.

Link to comment
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.