Firstly, the PHP:
As previously mentioned, you don't need quotes (") around the variables in your database connection, it's pointless.
You also need to get all the rows before JSON encoding.
Change:
echo json_encode(mysql_fetch_assoc($result));
To
$return = array();
while ($row = mysql_fetch_assoc($result))
{
$return[] = $row;
}
echo json_encode($return);
So that all the rows from the database are returned, instead of just the first.
Next for your jQuery:
Try changing: $('div').html(obj.attribute1+" "+obj.attribute2 ecc...);
To: $(obj.attribute1+" "+obj.attribute2 ecc...).appendTo('div');
Or: $('<div>' + obj.attribute1+" "+obj.attribute2 ecc... + '</div>').appendTo('body'); if you want each object in it's own container.
The way you are currently doing it will just overwrite the HTML for every div on the page for each iteration so that div elements will eventually only contain details for the last object in the collection.
Edit:
$.getJSON("demo.php",function(result){
var $id = 0;
$.each(result, function(i, obj){
$id ++;
$('<div id="obj' + $id + '"/>').appendTo('body');
$('<span class="var1">' + obj.var1 + '</span>').appendTo('div#obj' + $id);
$('<span class="var2">' + obj.var2 + '</span>').appendTo('div#obj' + $id);
$('<span class="var3">' + obj.var3 + '</span>').appendTo('div#obj' + $id);
$('<span class="var4">' + obj.var4 + '</span>').appendTo('div#obj' + $id);
});
});
Edit 2:
If you are trying to get each of the 'varX' from all elements into a single div, then just define the four divs (<div id="var1"/>, <div id="var2"/>, ...), and use this code:
$.getJSON("demo.php",function(result){
$.each(result, function(i, obj){
$(obj.var1).appendTo('div#var1');
$(obj.var2).appendTo('div#var2');
$(obj.var3).appendTo('div#var3');
$(obj.var4).appendTo('div#var4');
});
});
learn any single attribute in my html page