views:

570

answers:

4

I'm using the JQuery .ajax method to get the results of a simple PHP query in XML (which I've done various times within this project without a problem). However, the XML result that I receive from within my main project is different to what I expect (as tested by simply viewing the PHP file) when viewing the results in Firebug's console. I also tested this again by creating a new PHP file whose sole function is to run the ajax call and the results are correct, as expected.

[Update]: I just tested this out again, and I received the correct results from within my project, but when I refresh the page, I get the incorrect results again.

For the sake of testing, I simplified the process by removing all POST data and the success callback function, but I still get different results..

Here are the code snippets:

The JQuery ajax call (used in both the test file and within my project is):

    $.ajax({
        url:"./lib/ajax_friends.php",
        type:"POST",
     //   data:{action : "getFriends", userID: userID} ,
        dataType: 'xml',
        sync:false,
        error:function(request){alert("error")},
        success:function(theXML){
        }
   })

The PHP code is:

$userID='11';
    $sql = "SELECT ID, name, pic_square FROM users WHERE ID = $userID";
    $result = mysql_query($sql) or die(mysql_error());

    $xml = "";
    while($array = mysql_fetch_array($result)) {
     $ID = $array['ID'];
     $Name = $array['name'];
     $pic_square = $array['pic_square'];
     $xml .= "<Friend>";
     $xml .= "<ID>$ID</ID>";
     $xml .= "<Name>$Name</Name>";
     $xml .= "<Pic>$pic_square</Pic>";
     $xml .= "</Friend>";
    }

    header('Content-Type: application/xml; charset=ISO-8859-1');
    header("Cache-Control: no-cache, must-revalidate");
    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");

    echo "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>";
    echo "<Friends>";
    echo $xml;
    echo "</Friends>";

Note: I tried changing the headers to force a full refresh, but it still didn't help

Test File The headers from Firebug of the test PHP file (which returns the correct results are):

Response Headers
Date    

Fri, 08 May 2009 18:53:34 GMT

Server  

Apache/2.2.9 (Unix) mod_ssl/2.2.9 OpenSSL/0.9.7l DAV/2 PHP/5.2.6

X-Powered-By    

PHP/5.2.6

Cache-Control   

no-cache, must-revalidate

Expires 

Mon, 26 Jul 1997 05:00:00 GMT

Content-Length  

200

Keep-Alive  

timeout=5, max=98

Connection  

Keep-Alive

Content-Type    

application/xml; charset=ISO-8859-1

Request Headers
Host    

localhost

User-Agent  

Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.0.10) Gecko/2009042315 Firefox/3.0.10

Accept  

application/xml, text/xml, */*

Accept-Language 

en-us,en;q=0.5

Accept-Encoding 

gzip,deflate

Accept-Charset  

ISO-8859-1,utf-8;q=0.7,*;q=0.7

Keep-Alive  

300

Connection  

keep-alive

X-Requested-With    

XMLHttpRequest

Referer 

http://localhost/~Seeff/testajax.php

Cookie  

a7c768c2549daf4a7f69b9916bab5a38=4555bf36cf1b308f19a12f8da6944b80; a7c768c2549daf4a7f69b9916bab5a38_user
=507514167; a7c768c2549daf4a7f69b9916bab5a38_ss=kKgqsUlOrFArzo9Nrv2Zyg__; a7c768c2549daf4a7f69b9916bab5a38_session_key
=3.qPXakpbNIIX_bvndm_5gnA__.86400.1241895600-507514167; a7c768c2549daf4a7f69b9916bab5a38_expires=1241895600
; fbsetting_a7c768c2549daf4a7f69b9916bab5a38=%7B%22connectState%22%3A1%2C%22oneLineStorySetting%22%3A1
%2C%22shortStorySetting%22%3A1%2C%22inFacebook%22%3Afalse%7D

And the Response is:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><Friends><Friend><ID>12</ID><Name>XXX
</Name><Pic>http://something&lt;/Pic&gt;&lt;/Friend&gt;&lt;/Friends&gt;

Main Project The Firebug headers from the ajax call from my main project are:

Response Headers
Date    

Fri, 08 May 2009 18:53:41 GMT

Server  

Apache/2.2.9 (Unix) mod_ssl/2.2.9 OpenSSL/0.9.7l DAV/2 PHP/5.2.6

X-Powered-By    

PHP/5.2.6

Cache-Control   

no-cache, must-revalidate

Expires 

Mon, 26 Jul 1997 05:00:00 GMT

Content-Length  

74

Keep-Alive  

timeout=5, max=98

Connection  

Keep-Alive

Content-Type    

application/xml; charset=ISO-8859-1

Request Headers
Host    

localhost

User-Agent  

Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.0.10) Gecko/2009042315 Firefox/3.0.10

Accept  

application/xml, text/xml, */*

Accept-Language 

en-us,en;q=0.5

Accept-Encoding 

gzip,deflate

Accept-Charset  

ISO-8859-1,utf-8;q=0.7,*;q=0.7

Keep-Alive  

300

Connection  

keep-alive

X-Requested-With    

XMLHttpRequest

Referer 

http://localhost/~Seeff/

Cookie  

a7c768c2549daf4a7f69b9916bab5a38=4555bf36cf1b308f19a12f8da6944b80; a7c768c2549daf4a7f69b9916bab5a38_user
=507514167; a7c768c2549daf4a7f69b9916bab5a38_ss=kKgqsUlOrFArzo9Nrv2Zyg__; a7c768c2549daf4a7f69b9916bab5a38_session_key
=3.qPXakpbNIIX_bvndm_5gnA__.86400.1241895600-507514167; a7c768c2549daf4a7f69b9916bab5a38_expires=124
1895600

And the response is:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><Friends></Friends>

So for some reason, the inner XML node is being omitted from the results when called from the main project.

I'm guessing that there's something in my main project that must be interfering in some way, but I just can't figure it out. Any help is greatly appreciated!

A: 

My guess is you should be using mysql_fetch_assoc instead of mysql_fetch_array. mysql_fetch_array will give you an array structure with numeric indexes. Where mysql_fetch_assoc will give you the $array[column_name] like you are looking for.

atodd
Its a good point that you make, but I tried making the change and am still having the same problem..
A: 

I think that it's a mysql connection issue; the select is not running anyhow. To debug, add an echo "hello" to while{} , and see if it shows up at all or not :)

balint
it looks like the select is running, because when I run the PHP file by itself it returns the correct results but when I call it through AJAX, the results are different (even though no parameters are sent through)
try to post a some data with ajax. i had the same with a web service call on .net; I guess "getFriends" is a method, but it's not being called at all.
balint
A: 

In firebug, right click on the AJAX call, then select "Open in new tab." Firefox should format the XML nicely for you (if you are doing header("Content-Type: application/xml") like you should). Look at the request it is sending (in the location box). Reload it a few times. Print the data in the PHP file that you think should be set.

I'm sorry I can't help more but it sounds like some silly mistake somwhere.

Alternatively, you could just build the data up as an array, and then use JSON (my preferred method).

On more thing, you should NEVER do parameter injection into SQL. Bery very dangerous. Use something like PDO.

$userID='11';
$PDO = new PDO("mysql:dbname=mydb", "uname", "pword");

$stmt = $PDO->prepare("SELECT ID, name, pic_square FROM users WHERE ID = :userID");
$stmt->execute(array("userID" => $userID)) or die (print_r($PDO->errorInfo(), TRUE));

$output = array();
while($row = $stmt->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_NEXT)) {
    $output = $row;
}

header('Content-Type: application/json; charset=ISO-8859-1');
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");

print json_encode($output);
Paul Tarjan
A: 

Seems like you're not getting data from the db. What happends if you just do this

echo "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>";
echo "<Friends>";
echo "<Test>Hello</Test>";
echo "</Friends>";

instead? If it really is a cache problem, then adding a random param to the URL should force the browser to update. Ie does this should work:

$.ajax({
    url:"./lib/ajax_friends.php?random987398792374",
...

Good luck!

0scar