Closed Thread
Results 1 to 8 of 8

Thread: My scripts keep returning null...

  1. #1
    Jubal's Avatar
    Jubal is offline Newbie
    Join Date
    Jan 2009
    Posts
    22
    Rep Power
    0

    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. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Jaan Guest

    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" ?

  4. #3
    Jubal's Avatar
    Jubal is offline Newbie
    Join Date
    Jan 2009
    Posts
    22
    Rep Power
    0

    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.

  5. #4
    NatalieM's Avatar
    NatalieM is offline Learning Programmer
    Join Date
    Jun 2009
    Location
    London, England
    Posts
    83
    Rep Power
    10

    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.

  6. #5
    zeroradius's Avatar
    zeroradius is offline Speaks fluent binary
    Join Date
    Feb 2008
    Location
    Ohio
    Posts
    1,403
    Rep Power
    25

    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

  7. #6
    Jubal's Avatar
    Jubal is offline Newbie
    Join Date
    Jan 2009
    Posts
    22
    Rep Power
    0

    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.

  8. #7
    Join Date
    Sep 2007
    Location
    Karlstad, Sweden
    Posts
    3,082
    Blog Entries
    7
    Rep Power
    41

    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)
    __________________________________________
    I study Information Systems at Karlstad University when I'm not on CodeCall

  9. #8
    Jubal's Avatar
    Jubal is offline Newbie
    Join Date
    Jan 2009
    Posts
    22
    Rep Power
    0

    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.

Closed Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. help: it always say cannot be null
    By wheay in forum PHP Development
    Replies: 5
    Last Post: 05-13-2011, 01:58 PM
  2. A lot of NULL
    By BASHERS33 in forum Database & Database Programming
    Replies: 2
    Last Post: 04-11-2010, 08:40 AM
  3. Returning a value
    By moles in forum Python
    Replies: 1
    Last Post: 11-17-2009, 08:18 AM
  4. NULL
    By BASHERS33 in forum Database & Database Programming
    Replies: 23
    Last Post: 05-27-2009, 02:45 AM
  5. Object is Null
    By Lop in forum C# Programming
    Replies: 2
    Last Post: 09-21-2006, 09:25 AM

Tags for this Thread

Bookmarks

Posting Permissions

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