After a long hiatus I'm back programming. I'm writing a hiscores script for my game, but it always returns null. Anyone got any idea how it can be fixed?
HTML/JS:
I think it is this file in which the problem rsides probably, it's for finding the highest pop;Code:<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title>The Team</title> <meta http-equiv="Page-Enter" content="blendTrans(Duration=1.0)"> <meta http-equiv="Page-Exit" content="blendTrans(Duration=1.0)"> <script> var popname = "nobody"; var city = "nowhere"; var pop = 0; var xmlHttp = createXmlHttpRequestObject(); //////////////// //////////////// //////////////// function popandciv(){ parent.location.href="index.html"; } function popandciv2(){ parent.location.href="http://web.aanet.com.au/toddmarshall/"; } //////// //////// //////// //////// function createXmlHttpRequestObject() { var xmlHttp; if(window.ActiveXObject) { //try //{ xmlHttp = new ActiveXObject("Microsoft.XMLHTTP") //} //catch(e) //{ //xmlHttp = false; //} } else { //try //{ var xmlHttp = new XMLHttpRequest(); //} //catch(e) //{ //xmlHttp = false; //} } if (!xmlHttp) window.alert("Error makin stuff work, sesifckally creatin' the Haitch Tee Tee Pee requesterization hobject sah..."); else return xmlHttp; } //////// //////// //////// ///////// FIND HIGHEST POP //////// //////// function hiscorepop() { //window.alert("processx started!") if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0) { //window.alert("Server ready, sending message!") nameserv = name; xmlHttp.open("GET", "hspop.php", true); xmlHttp.onreadystatechange = handleServerResponsehiscorepop; xmlHttp.send(null); } else setTimeout ('process()', 1000) } function handleServerResponsehiscorepop() { if (xmlHttp.readyState == 4) { if (xmlHttp.status == 200) { jsonResponse = eval(xmlHttp.responseText); window.alert(jsonResponse); popname = jsonResponse; plupdate(); processpop(); } else { window.alert("H'im afarid I can not get into the server, m'lud. (xval) Aplolgies." + xmlHttp.statusText); } } } function processpop() { ////window.alert("processx started!") if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0) { ////window.alert("Server ready, sending message!") nameserv = name; xmlHttp.open("GET", "adbpop.php?name=" + popname, true); xmlHttp.onreadystatechange = handleServerResponsepop; xmlHttp.send(null); } else setTimeout ('process()', 1000) } function handleServerResponsepop() { if (xmlHttp.readyState == 4) { //window.alert("SERVER READY! (xval)"); if (xmlHttp.status == 200) { ////window.alert("MESSAGE RECIEVED!"); jsonResponse = eval(xmlHttp.responseText); ////window.alert(jsonResponse); newx = jsonResponse; window.alert(newx); pop = newx; window.alert(pop); popupdate(); processcityname(); //defaultx = newx; ////window.alert(defaultx); //xmymove(newx, 20); ////window.alert(newx); } else { window.alert("H'im afarid I can not get into the server, m'lud. (xval) Aplolgies." + xmlHttp.statusText); } } } function processcityname() { //window.alert("processx started!") if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0) { //window.alert("Server ready, sending message!") nameserv = name; xmlHttp.open("GET", "adbcityname.php?name=" + popname, true); xmlHttp.onreadystatechange = handleServerResponsecityname; xmlHttp.send(null); } else setTimeout ('process()', 1000) } function handleServerResponsecityname() { if (xmlHttp.readyState == 4) { //window.alert("SERVER READY! (xval)"); if (xmlHttp.status == 200) { //window.alert("MESSAGE RECIEVED!"); jsonResponse = eval(xmlHttp.responseText); //window.alert(jsonResponse); newx = jsonResponse; //window.alert(newx); city = newx; //window.alert(city); cityupdate(); //defaultx = newx; ////window.alert(defaultx); //xmymove(newx, 20); ////window.alert(newx); } else { window.alert("H'im afarid I can not get into the server, m'lud. (xval) Aplolgies." + xmlHttp.statusText); } } } function plupdate(){ var he = document.getElementById("plpop"); he.firstChild.nodeValue = popname + "'s mighty city of "; } function cityupdate(){ var he = document.getElementById("city"); he.firstChild.nodeValue = city; } function popupdate(){ var he = document.getElementById("popdiv"); he.firstChild.nodeValue = "With a population of " + pop; } </script> <link rel="stylesheet" type="text/css" href="./menus.css" /> </head> <body bgcolor="#000000" background="./images/backfull.jpg" text="#ffffff" onload="hiscorepop()"> <center><h1><strong>Raiders and Traders</strong></h1> </center> <center><h1><strong>Hiscores</strong></h1> <p> The largest city in Ithrala is <div id="plpop">Joe</div><div id="city">Leeds</div><div id="popdiv">0</div></p> </center> </body> </html>
I can post adbcityname and adbpop if it'd be helpful, but they're just data-grabbing scripts of quite a general form.Code:<?php $conn = mysql_connect('www.wavcott.org.uk', 'st628jg_exilian', 'exilian'); mysql_select_db("st628jg_test", $conn); $sql = "SELECT name, MAX(pop) FROM raiders"; $result = mysql_query($sql, $conn) or die(mysql_error()); while ($newArray = mysql_fetch_array($result)){ $x = $newArray['name']; } header('Content-Type: text/javascript'); echo json_encode($x); ?>
Thanks.
I may look like an idiot - and I may talklike an idiot - but don't ever let that fool you. I really am an idiot.
MySQL's hostname was weird.. isn't it "localhost" ?Code:<?php
$conn = mysql_connect('wavcott.org.uk', 'username', 'password');
mysql_select_db("test", $conn);
$sql = "SELECT name, MAX(pop) FROM raiders";
$result = mysql_query($sql, $conn) or die(mysql_error());
while ($newArray = mysql_fetch_array($result)){
$x = $newArray['name'];
}
header('Content-Type: text/javascript');
echo json_encode($x);
?>
Only if it's on the local host (i.e. the script and DB are in the same place)... this way I can run it wherever the files are should I want to (localhost should work where it is now, but I want to be prepared). All my other scripts use the same login details and work fine.
I may look like an idiot - and I may talklike an idiot - but don't ever let that fool you. I really am an idiot.
Shouldn't you use a GROUP BY clause for your SQL statement, as you have used a column in your SELECT clause not encapsulated by your MAX clause?
As in
As I'm not sure how your table is built, I might be wrong here?Code:$sql = "SELECT name, MAX(pop) FROM raiders GROUP BY name";
Have you tested your php script by including a echo function on one of the fields name in the while loop? This is normally how I debug my mistakes, just use echo to work out where things do not go as expected.
I'm not sure what it is exactly you are trying to do with this but changeing the following returns a value (tested using my db) this returns a value. If you give us a bit more o what you are trying to do i can modify it to do exactly what you are looking for
select statemnt was wrong should be: edit: change u_id to the column you want the value from and change users back to your table
Once you do that change the following:Code:$sql = "SELECT MAX(u_id) AS maxid FROM users";
Code:$x = $newArray['maxid'];
Edit: Sorry just re-read your post and see that you said what you were doing. I presume that you are trying to get a list of the top X high scores? If so you would be better off doing a regular select statement that returns in descending order (will put top score on top and than go down the list by score from there) use Limit to specify how many you want to return
Being specific, I need to return the "name" for the user with the highest value of "pop".
How do I do a "normal select statement" as you put it? I'm very inexperienced with SQL code.
I may look like an idiot - and I may talklike an idiot - but don't ever let that fool you. I really am an idiot.
This returns the row with highest value on field pop:
this orders the table highest to lowest (ORDER BY pop DESC) and returns the very first row (LIMIT 1)Code:SELECT * FROM users ORDER BY pop DESC LIMIT 1
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall
Thank you ever so much. Finally the **** thing works.
I may look like an idiot - and I may talklike an idiot - but don't ever let that fool you. I really am an idiot.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks