原生php遍历获取查出来的数据,进行输出

连接方法:$db=mysqli_connect("127.0.0.1","用户名","密码","数据库名")or die("Mysql连接失败" . mysqli_connect_error());

$sql="sql语句";

$res=$db->query($sql);

$str='';

foreach($res as $k => $v){

  $str .=implode(',',$v) . '-';
}

$str =substr($str ,0, strlen($str) -1);

echo $str;

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

输出json格式

$arr=array();

foreach($res as $k => $v){

  if($v['open']==0){

    $list[$k]['id'] = $v['id'];

    箭值对格式

  } 

  $list[$k]['id'] = $v['id']; 

}

$json =json_encode($list);

echo $json;

 

 

-----------------------------------------------------------------------------------------------------------------------------------

ajax返回在页面上输出

function fangru(arr){

//arrj就是查出来的数据以‘-’分割

var brr=[];

var str ="";

for(var i =0;i<arr.length;i++){

  brr=arr[i].split(',');

  str +='<li onclick=\" xiangqing('+brr[0]+')\">\n'+

     '      <div class=\"a-content\">\n' +

       '            <img class=\"left\" src=" '+brr[1]+' ">\n'+

     '                     <h3 class=\"right\">\n'+

     brr[3] +

     '                       </h3>\n'+

     '        </div>\n'+

     '<li>';

}

//然后把数据放入到html里面

  $('.a-content').html(str);

}

posted @ 2019-10-22 09:19  聽丶  阅读(2066)  评论(0编辑  收藏  举报