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
- Create a connection to a mysql Database
- Put the results in an array
- encode to JSON
- 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.
 
    