Results 1 to 2 of 2

Thread: using json to pull database into html pages.

  1. #1
    Join Date
    Feb 2005
    Posts
    153
    Thanks
    3
    Thanked 2 Times in 2 Posts

    Default using json to pull database into html pages.

    Im trying to build an app for iPhone which I can package up with phone gap. I know how to do the basic structure but im trying to get my html/javascript pages to connect to a database. Im doing this through json.


    basically I have a php page which pulls fields from the database:
    PHP Code:
    <?php
       
    // Database credentials
       
    $host 'xx'
       
    $db 'xx'
       
    $uid 'xx'
       
    $pwd 'xx';
      
    // Connect to the database server   
        
    $link mysql_connect($host$uid$pwd) or die("Could not connect");
       
       
    //select the json database
       
    mysql_select_db($db) or die("Could not select database");
       
       
    // Create an array to hold our results
       
    $arr = array();
       
    //Execute the query
       
    $rs mysql_query("SELECT id,userid,firstname,lastname,email FROM users");
       
       
    // Add the rows to the array 
       
    while($obj mysql_fetch_object($rs)) {
       
    $arr[] = $obj;
       }
       
       
    //return the json result. The string users is just a name for the container object. Can be set anything.
       
    echo '('.json_encode($arr).');';
       
       
    ?>
    If you view the page it gives you the json query: http://www.garethmoore.net/jq.php
    ([{"id":"1","userid":"fhardy","firstname":"Frank","l astname":"Hardy","email":"fhardy@hauntedclock.com"},{"id":"2","userid":"jhar dy","firstname":"Joe","lastname":"Hardy","email":" jhardy@hauntedclock.com"},{"id":"3","userid":"ndre w","firstname":"Nancy","lastname":"Drew","email":" ndrew@hauntedclock.com"},{"id":"4","userid":"sdoo" ,"firstname":"Scooby","lastname":"Doo","email":"sd oo@mysterymachine.com"}]);

    Then I have my HTML page (which is just stored on my loacl hard drive. This is the HTML/Javascript code:
    [HTML]
    <html>
    <head>

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.js"></script>

    <script type="text/javascript">
    $(document).ready(function(){
    $.getJSON('http://www.garethmoore.net/jq.php?count=20&callback=?', function(data){
    $.each(data, function(index, item){
    $('#twitter').append('<div class="tweet"><p>' + item.email + '</p></div>');
    });

    });

    });
    </script>

    </head>

    <body>
    <div id="twitter" >

    </div>

    </body>
    </html>
    [/HTML]

    Ive pretty much taken the HTML/javascript from a Twitter feed that ive used loads before and it works. But for some reason, even though the code is the same, and the json output is the same (as the twitter output) this just doesnt seem to work.

  2. #2
    Join Date
    Jun 2005
    Posts
    270
    Thanks
    12
    Thanked 11 Times in 9 Posts

    Default

    I'm not sure why you'd add "count=20&callback=?" in the query string of your JSON request. Did you try using a debugger (e.g. Firebug) to see what is returned in the JSON response?

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Blank pages served
    By Rappie in forum General Technical Support
    Replies: 3
    Last Post: 26th March 2009, 08:28 AM
  2. how to fetch external pages
    By chet in forum Website and Graphic Design
    Replies: 2
    Last Post: 11th August 2008, 10:13 PM
  3. Php processing html pages
    By Viktor in forum General Technical Support
    Replies: 1
    Last Post: 3rd January 2007, 10:54 PM
  4. Custom error pages.....
    By jigsawsrusdotcom in forum Sales and Service Feature Enquiries
    Replies: 5
    Last Post: 17th November 2006, 03:54 PM
  5. Yellow pages
    By Jaro in forum Portal/CMS Applications
    Replies: 2
    Last Post: 29th December 2005, 11:38 AM

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
  •