首页 新闻 会员 周边 捐助

求大神解答php从mysql里面的数据转化成数组,然后传回json再拼成<li>

0
[已关闭问题] 解决于 2015-05-22 13:39
<div class="rotary">
    <div class="rotaryArrow" id="rotaryArrow"></div>
    <div class="list">
        <ul id="li">
        </ul>
    </div>
 <script type="text/javascript">
    $.ajax({
        type:'POST',
        url:'test1.php',
        data:['li'],
        dataType: 'json',
        cache:false,
        error: function(){ 
            alert('出错了!'); 
            return false; 
        },
success: function(json){
    var list = '';
    for(var o in json){
        list += json[o];
    }

    $("#li").html(list);
}
    });

要求返回格式是json,并且需求只返回数组,再由html里面拼成<li>

<?php
$servername = "localhost";
$username = "root";
$password = "123";
$dbname = "list";
$conn = new mysqli($servername, $username,$password,$dbname);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
$sql = "SELECT num FROM tele";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
    $li='';
    while($row = $result->fetch_assoc()) {
        $li .= '<li>'. $row["num"].'</li>';
    }
} else {
    echo "0 results";
}

$conn->close(); 
echo json_encode($li); 
?>

dudu大神快来就我

YaYou~的主页 YaYou~ | 初学一级 | 园豆:6
提问于:2015-05-22 11:35
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册