Hello,
I'm working on a mobile app (I'm a total newbie) and I need some help. I'm trying to fetch data from MySQL database and encode it in JSON and then get it with AJAX call in jQuery.
HTML:
jQuery:
Code:
$(document).ready( function() {
done();
});
function done() {
setTimeout( function() {
updates();
done();
}, 200);
}
function updates() {
$.ajax({
url:"http://wawhost.com/appProject/fetch.php",
dataType: 'jsonp',
success:function(data){
$("ul").empty();
$.each(data.result, function(){
$("ul").append("<li>Name: "+this['name']+
"</li><li>Age: "+this['age']+
"</li><li>Company: "+this['company']+
"</li><br />");
});
};
});
}
PHP:
Code:
include_once('db.php');
$sql = "SELECT * FROM posts";
$res = mysql_query($sql);
$result = array();
while( $row = mysql_fetch_array($res) )
array_push($result, array('name' => $row[0],
'age' => $row[1],
'company' => $row[2]));
echo json_encode(array("result" => $result));