Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Apr 2013
    Posts
    157
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Question JSON outputs NULL

    Can anyone help me here?

    this page does not work since my host did a migration to a different server:

    http://www.mypubspace.com/pubsmobile...sTown=brighton
    PHP Code:
    <?php

    include 'configure.php';


    $town $_GET["rsTown"];
    $qr "SELECT * FROM pubs WHERE rsTown = '$rsTown' ORDER BY rsPubName ASC";



    $resmysql_query($qr);
    $i=0;
    while(
    $row mysql_fetch_array($res))
    {
    $stud_arr[$i]["PUBID"] = $row["PUBID"];
    $stud_arr[$i]["rsAddress"] = $row["rsAddress"];
    $stud_arr[$i]["Add2"] = $row["Add2"];
    $stud_arr[$i]["Add3"] = $row["Add3"];
    $stud_arr[$i]["rsTown"] = $row["rsTown"];
    $stud_arr[$i]["rsCounty"] = $row["rsCounty"];
    $stud_arr[$i]["rsPubName"] = $row["rsPubName"];
    $stud_arr[$i]["rsPostCode"] = $row["rsPostCode"];
    $stud_arr[$i]["rsTel"] = $row["rsTel"];
    $stud_arr[$i]["rsWebsite"] = $row["rsWebsite"];
    $stud_arr[$i]["Region"] = $row["Region"];
    $stud_arr[$i]["rsAboutpub"] = $row["rsAboutpub"];
    $i++;
    }
    header('Access-Control-Allow-Methods: POST, GET, OPTIONS'); // set the methods you want
    header('Access-Control-Allow-Origin: *'); // this allows access from any domain
    header('Content-type: application/json');

    echo 
    json_encode($stud_arr);

    ?>

    Whereas this page does work:
    http://www.mypubspace.com/pubsmobile...?PUBID=1155833
    PHP Code:
    <?php

    include 'configure.php';

    $stud_id $_GET["PUBID"];
        
        
    $qr "SELECT * FROM jbiddulph.pubs WHERE PUBID = '$stud_id'";


    $getmsg "SELECT * FROM pubs WHERE PUBID = $stud_id";      
    $result mysql_query($getmsg) or die("Error: " mysql_error());
    $result mysql_fetch_assoc($result);

    header('Access-Control-Allow-Methods: POST, GET, OPTIONS'); // set the methods you want
    header('Access-Control-Allow-Origin: *'); // this allows access from any domain
    header('Content-type: application/json');

    $detailsarray = Array();
    $detailsarray[0] = $result;
    echo 
    json_encode($detailsarray);
    ?>

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,202
    Thanks
    80
    Thanked 4,564 Times in 4,528 Posts
    ??? Why do you say it does not work. It looks right to me.

    Maybe it is your JS code that is messed up? The PHP code seems to me to be creating a correct JSON encoding of all the pubs in Brighton.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,202
    Thanks
    80
    Thanked 4,564 Times in 4,528 Posts
    Not to ask a dumb-a-s-s question, but...

    Why do you do
    header('Access-Control-Allow-Methods: POST, GET, OPTIONS');
    when in fact the only method you support in that code is GET??

    You explicitly code $_GET so no other method will actually work.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,202
    Thanks
    80
    Thanked 4,564 Times in 4,528 Posts
    I did notice that the first code does not use *ALL* fields from each record. The second code, for example, will get img1, img2, img3. Maybe other fields. I didn't check.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Tags for this Thread

    Posting Permissions

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