+ Reply to Thread
Results 1 to 8 of 8

Thread: My scripts keep returning null...

  1. #1
    Newbie Jubal is an unknown quantity at this point Jubal's Avatar
    Join Date
    Jan 2009
    Posts
    22

    My scripts keep returning null...

    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:
    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 think it is this file in which the problem rsides probably, it's for finding the highest pop;
    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);
    ?>
    I can post adbcityname and adbpop if it'd be helpful, but they're just data-grabbing scripts of quite a general form.

    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.

  2. #2
    Moderator Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan's Avatar
    Join Date
    Dec 2006
    Location
    Estonia
    Age
    18
    Posts
    2,048
    Blog Entries
    9

    Re: My scripts keep returning null...

    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);
    ?>
    MySQL's hostname was weird.. isn't it "localhost" ?
    Trill Hosting - Cheap Web Hosting, Register Cheap Domains, Cheap Blog Hosting
    www.trillhosting.com | support@trillhosting.com
    Hosting Plans | Write To Us | Support | Client Area | About Us
    CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum | Freelance

  3. #3
    Newbie Jubal is an unknown quantity at this point Jubal's Avatar
    Join Date
    Jan 2009
    Posts
    22

    Re: My scripts keep returning null...

    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.

  4. #4
    Learning Programmer NatalieM is on a distinguished road NatalieM's Avatar
    Join Date
    Jun 2009
    Location
    London, England
    Age
    33
    Posts
    73

    Re: My scripts keep returning null...

    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

    Code:
    $sql "SELECT name, MAX(pop) FROM raiders GROUP BY name"
    As I'm not sure how your table is built, I might be wrong here?

    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.

  5. #5
    Programming God zeroradius will become famous soon enough zeroradius's Avatar
    Join Date
    Feb 2008
    Location
    Ohio
    Posts
    823

    Re: My scripts keep returning null...

    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
    Code:
    $sql = "SELECT MAX(u_id) AS maxid FROM users";
    Once you do that change the following:
    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


  6. #6
    Newbie Jubal is an unknown quantity at this point Jubal's Avatar
    Join Date
    Jan 2009
    Posts
    22

    Re: My scripts keep returning null...

    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.

  7. #7
    Moderator Orjan is a glorious beacon of light Orjan is a glorious beacon of light Orjan is a glorious beacon of light Orjan is a glorious beacon of light Orjan is a glorious beacon of light Orjan's Avatar
    Join Date
    Sep 2007
    Location
    Karlstad, Sweden
    Age
    34
    Posts
    2,607
    Blog Entries
    7

    Re: My scripts keep returning null...

    This returns the row with highest value on field pop:
    Code:
    SELECT * FROM users ORDER BY pop DESC LIMIT 1
    this orders the table highest to lowest (ORDER BY pop DESC) and returns the very first row (LIMIT 1)
    __________________________________________
    Ask me: Orjan | Contribute to the Wiki! | Make your own Programming blog!
    I usually play eRepublik and Travian when I'm not on CodeCall

  8. #8
    Newbie Jubal is an unknown quantity at this point Jubal's Avatar
    Join Date
    Jan 2009
    Posts
    22

    Re: My scripts keep returning null...

    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.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. Using Set Theory to Understand SQL Joins
    By WingedPanther in forum Tutorials
    Replies: 18
    Last Post: 02-27-2009, 04:01 PM
  2. making a simple discusion forum part 1 (the database)
    By omar_a_k in forum PHP Tutorials
    Replies: 3
    Last Post: 01-14-2009, 01:24 PM
  3. Is it worth learning C now?
    By Kaabi in forum C and C++
    Replies: 114
    Last Post: 11-18-2008, 09:16 PM
  4. [Fixed] Error during install
    By polyvios_s in forum ionFiles
    Replies: 4
    Last Post: 04-28-2008, 05:34 PM
  5. An important exam!
    By Zael in forum C and C++
    Replies: 12
    Last Post: 01-22-2008, 09:08 PM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts