PDA

View Full Version : Making XML with PHP, data from database.



Hunter
April 9th, 2010, 02:51 PM
Having a problem with a script, here is what I posted on another website:


Hey, another problem within the development of my chat system. I can't seem to get my PHP code to fetch the messages from the database :S

Although, from what I can see everything matches up...

The current URL which is being used to get the data:
http://martynball.byethost5.com/web10/scripts/get_data.php?uid=3&m=0&chat=1

PHP (I am aware that the get_name_query is wrong and will get the wrong name, that is not the problem at hand though):


<?php
include "connect.php";
mysql_select_db($dbname, $con);

//Variables
$ses = explode(".", $_SESSION['user']);
$sid = $ses[2]; //User ID of user currently logged in
$last_mess = (isset($_GET['m']) && $_GET['m'] != "") ? $_GET['m'] : 0;
$last_mess = mysql_real_escape_string($last_mess);
$chat = mysql_real_escape_string($_GET['chat']);

$xml = '<?xml version="1.0" ?><root>';
$query = "SELECT * FROM message WHERE chat_id = ".$chat." AND msg_id > ".$last_mess;
$result = mysql_query($query);
while ($row=mysql_fetch_array($result)) {

//Get name of sender
$get_name_query = mysql_fetch_array("SELECT * FROM users");
while ($row2=mysql_fetch_array($get_name_query)) {
$name = explode(".",$row['full_name']); $first_name = $name[0];
$sent_by = "<a href=\"../profile?user=".$row2['uid']."\">".ucfirst($first_name)."</a>";
}

//Write message contents
$xml .= '<message id="' . $row['msg_id'] . '">';
$xml .= '<user>' . $sent_by . '</user>';
$xml .= '<text>' . $row['message']) . '</text>';
$xml .= '<time>' . $row['post_time'] . '</time>';
$xml .= '</message>';
}
$xml .= '</root>';
echo $xml;
?>

AJAX if you need it, shouldnt do as that is not the problem:



<script type="text/javascript">
//Credits//
//Help: Greg (Gjslick)
//Code example: http://www.dynamicajax.com/fr/AJAX_Driven_Web_Chat_Backend-271_290_291_296.html

// Declare Global Variables
var lastMess;
var sendReq;
var receiveReq;
var mTimer;

// Initialize Global Variables (on page load)
function init() {
lastMess = 0;
sendReq = getXmlHttpRequestObject();
receiveReq = getXmlHttpRequestObject();
mTimer;
}


function getXmlHttpRequestObject() {
if( window.XMLHttpRequest ) {
return new XMLHttpRequest(); // code decent browsers like Firefox, Chrome, Opera, Safari. And IE7+...

} else if( window.ActiveXObject ) {
return new ActiveXObject( 'Microsoft.XMLHTTP' ); //Code for crap like IE

} else {
var statusEl = document.getElementById( 'p_status' );
statusEl.innerHTML = "Status: Could not create XmlHttpRequest Object, please upgrade your browser!";

return null;
}
}

function getMessage() {
var url = "scripts/get_data.php?uid=<?=$uid?>&m=" + lastMess + "&chat=1";
receiveReq.open("GET",url,true);
receiveReq.onreadystatechange = manageMessage;
receiveReq.send( null );
lastMess++;
}

function manageMessage() {
if( receiveReq.readyState == 4 ) { // request complete
var chat_div = document.getElementById( 'chat' );
var xmldoc = receiveReq.responseXML;
var msg_nodes = xmldoc.getElementsByTagName( 'message' );
var n_msg = msg_nodes.length;
for( i = 0; i < n_msg; i++ ) {
var sender_node = msg_nodes[ i ].getElementsByTagName( 'sender' );
var receiver_node = msg_nodes[ i ].getElementsByTagName( 'receiver' );
var time_node = msg_nodes[ i ].getElementsByTagName( 'time' );
var text_node = msg_nodes[ i ].getElementsByTagName( 'text' );

//Display message
chat_div.innerHTML += "Here: <br />" + sender_node[ 0 ].firstChild.nodeValue + "<br />";
chat_div.innerHTML += receiver_node[ 0 ].firstChild.nodeValue + "<br />";
chat_div.innerHTML += time_node[ 0 ].firstChild.nodeValue + "<br />";
chat_div.innerHTML += text_node[ 0 ].firstChild.nodeValue + "<br /><br />";
}
}
}
</script>


Database Data:
http://i58.photobucket.com/albums/g268/martynball/database_view.jpg

Hunter
April 9th, 2010, 03:49 PM
Why is there no one that can help me? Lol. I still have no replies on the thread on codingforums.com which is dedicated for coding problems. Damn.