prova così

codice:
<script type="text/javascript"> 
	function doChange(){
		if (document.body){
			var backImage = new Array();
			<?php
				$n = 0;
				while ($row = mysql_fetch_array($result)) {?>
			backImage[<?php echo $n; ?>] = "sfondi/<?php echo $row['src_sfondo']; ?>";
			<?php 
				$n++;
				}
			?>
			ran = Math.floor(<?php echo $n; ?> * Math.random());
			document.body.style.backgroundImage = 'url('+backImage[ran]+')';
			}
	    }
    }
  </script>