当前位置:Gxlcms > PHP教程 > php如何获取数据

php如何获取数据

时间:2021-07-01 10:21:17 帮助过:22人阅读

<?php

  1. header("Content-type:text/html;charset=utf-8");//字符编码设置
  2. $servername = "localhost";
  3. $username = "root";
  4. $password = "root";
  5. $dbname = "web";
  6. // 创建连接
  7. $con =mysqli_connect($servername, $username, $password, $dbname);
  8. // 检测连接
  9. $sql = "SELECT * FROM users";
  10. $result = mysqli_query($con,$sql);
  11. if (!$result) {
  12. printf("Error: %s\n", mysqli_error($con));
  13. exit();
  14. }
  15. $jarr = array();
  16. while ($rows=mysqli_fetch_array($result,MYSQL_ASSOC)){
  17. $count=count($rows);//不能在循环语句中,由于每次删除 row数组长度都减小
  18. for($i=0;$i<$count;$i++){
  19. unset($rows[$i]);//删除冗余数据
  20. }
  21. array_push($jarr,$rows);
  22. }
  23. echo $str=json_encode($jarr);//将数组进行json编码
  24. ?>

以上就是php如何获取数据的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行