0

Good Morning,

I am bit confused on the process of these 2 as i am quite new. Any guidance or any available tutorial given is really appreciated.

Now what i am trying to achieve is

  1. Create a connection to a mysql Database
  2. Put the results in an array
  3. encode to JSON
  4. decode string from JSON and load to jQuery

this is what I've done so far

database.php

<?php
    function getDbConnection() {
        $db = new PDO(DB_DRIVER . ":dbname=" . DB_DATABASE . ";host=" . DB_SERVER, DB_USER);
        $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
        return $db;
    }

    //this is for loading ratings 
    function home_ratings()
    {
        $db = getDbConnection();
        $stmt = $db->prepare("select * from ratings");                    
        $isQueryOk = $stmt->execute();
        $results_ratings = array();

        if ($isQueryOk) 
        {
            $results_ratings = $stmt->fetchAll(PDO::FETCH_COLUMN);
        } 
        else 
        {
            trigger_error('Error executing statement.', E_USER_ERROR);
        }
        $db = null; 
        return $results_ratings;
    }
?>

getratings.php

<?php
    require('constant.php'); //my database credentials
    require('database.php');

    $data = home_ratings();
    echo json_encode($data);
?>

Now I have tried to load the data using JS but I get an undefined error.

ratings.js

$(document).ready(function() {
    $.get("php/get_ratings.php").done(function(data) {
        $('#home').html('');
        var results = jQuery.parseJSON(data);
        $.each(results, function(key, value) {
            alert(results.rating);
        })
    });
});

Any help is really appreciated.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
user1805445
  • 159
  • 1
  • 10
  • 1
    `but i get an undefined error`, which error and where ? – empiric Jul 13 '15 at 09:37
  • 4
    *"but i get an undefined error"* Can you elaborate on that? Why are you iterating over `results` but also try to access `results.rating`? That doesn't make sense. If you don't know how `$.each` works, [read its documentation](http://api.jquery.com/jquery.each/). However, I think you should read [Access / process (nested) objects, arrays or JSON](http://stackoverflow.com/q/11922383/218196) – Felix Kling Jul 13 '15 at 09:37
  • ill go through the documentation , i am getting the error on the alert output. To my knowledge i assumed that you can go over columns , i left it to results only and i get the first column output. – user1805445 Jul 13 '15 at 09:41
  • I believe you are not getting an error at all, the alert simply shows `undefined`. tl;dr, use `value.rating` instead. – Felix Kling Jul 13 '15 at 09:42
  • Thank you Felix, using rating or value showed up my first column on my query but how can i view the remaining? – user1805445 Jul 13 '15 at 09:46
  • Depends on what value `results` is. If it is an array (which I assumed it is), `$.each` will iterate over each element in the array and let you access it. The linked question explains all of this. – Felix Kling Jul 13 '15 at 09:54

0 Answers0