0

I have been working on my application that is meant to return data from a sql query from a PHP file to the client C# application.

The query is executed on the PHP side and loaded into a referenced XML query where it is sent to the client. The client then reads the XML and extracts the required data.

However, I am a tad confused. For some reason - not all information is being returned.

PHP:

 $query = "SELECT * FROM Parks";
 $result = mysql_query($query);
 while($row = mysql_fetch_array($result))
 { 
$array = array (
 $row['Name'] => 'Name',
 $row['File'] => 'File', 
 $row['Image'] => 'Image',
 $row['Description'] => 'Description',
 $row['Author'] => 'Author',
 $row['CS'] => 'CS',
 $row['Size'] => 'Size',
 $row['Theme'] => 'Theme',
 $row['Mountains'] => 'Mountains',
 $row['Hills'] => 'Hills',
 $row['Rivers'] => 'Rivers',
 $row['Lake'] => 'Lake',
 $row['Ocean'] => 'Ocean',
 $row['Island'] => 'Island',
 $row['SetOfIslands'] => 'Setofislands',
 $row['Waterfalls'] => 'Waterfalls',
 $row['Download'] => 'Download',
 $row['ID'] => 'ID',
 $row['Rating'] => 'Rating',
 $row['FileSize'] => 'FileSize',
 $row['DateUploaded'] => 'Dateuploaded',
 );
 $xml = new SimpleXMLElement('<Parks/>');
 array_walk_recursive($array, array ($xml, 'addChild'));
 echo $xml->asXML(); 

However, on the client side the XML is missing certain values.

For example, Run 1 returned the values of: Name,FileImage,Description,Author,CS,Size,Theme,Lake,Waterfalls,Dateuploaded,Download,ID,Rating,FileSize

While Run 2 returned: Name,File,Image,Description,Author,CSSize,Theme,Waterfalls,Rivers,Dateuploaded,Download,ID,Rating,FileSize

As you can see the first run doesn't return the parameter 'Rivers' but the second one does. Every run seems to return different values.

After checking my database all of the values it was missing were stored either as a 'True' or 'False' variable. I'm not sure if that has any thing to do with it - but checking the returned results against the data in my database I found there was no consciency.


In short, why isn't the array storing all of the data I'm asking it to and why does the process of what it chooses to store seem to be so random?

PS: I've echoes back each row before and can confirm the database rows are named correctly and data is present within them....


EDIT: An example of the returned xml:

 <?xml version="1.0"?>
<Parks><Name>Some Name</Name>
<File>The File Path</File>
<Image>Link To Screenshot</Image>
<Description>Random description </Description>
<Author>Someone</Author>
<CS>False </CS><
Size>small</Size>
<Theme>space</Theme>
<Island>False</Island>
<Lake>True</Lake>
<Ocean>False</Ocean>
<Waterfalls>True</Waterfalls>
<Rating>0</Rating>
<ID>166</ID>
<FileSize>3706121</FileSize>
<Dateuploaded>2013年04月04日</Dateuploaded>
</Parks>
asked May 15, 2013 at 13:59
6
  • 2
    Shouldn't your keys come before the value. key=>value Commented May 15, 2013 at 14:00
  • By the way, you should use mysql_fetch_assoc instead of mysql_fetch_array. Commented May 15, 2013 at 14:01
  • There is no ] after "Rivers", surely a typo, but take a look. Commented May 15, 2013 at 14:03
  • 1
    you shouldn't be using mysql_* you should upgrade to mysqli_* instead. I think @karmafunk has the answer, you are probably missing the element names for those records that the value is not valid as a xml element names. Commented May 15, 2013 at 14:04
  • voitcus - Oops. Well spotted. That's a mistake in this post not the actual code. The code has the ']' currently. @karmafunk - That is the way I thought it should be myself. However, else where in my application it works perfectly fine. In fact, the returned data is in the format <value>key</value> It seems to swap them around when it exports it as XML :S Commented May 15, 2013 at 14:23

1 Answer 1

1

The problem is having the same value for multiple columns in a row in the database, and using these values as indexes of the array.

if you had this row in the db:

File | Image | Rivers | Whatever
--------------------------------
1 | 1.jpg | true | true

Generating your array with:

while($row = mysql_fetch_array($result)) { 
 $array = array (
 $row['File'] => 'File', 
 $row['Image'] => 'Image',
 $row['Rivers'] => 'Rivers',
 $row['Whatever'] => 'Whatever'
 );
}

It will miss Rivers again, because both Rivers and Whatever are going to the same index of the array (true in this case), so the latter value overwrites the former.

So, create the array like @karmafunc said in a comment, key => value, and use array_keys()` to get the keys.

answered May 15, 2013 at 14:42
1
  • This makes a lot of sense. It just because I have the key and value the wrong way around and thus, it's just overwriting each other... Unfortunatly, the code I have to convert a array to a xml needs to work this way around. However, now I know the issue I am sure I can make a quick work around :) Thanks for the answeR! Commented May 15, 2013 at 15:10

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.