Jump to content

PHP XLM/JSON API problem with mySQL connect include


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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.