您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

php获取数据库中数据的实例代码

2024/6/2 12:32:53发布26次查看
配置好了php环境,接下来,我们要正式开始对数据库进行操作了!首先是读取mysql中的数据。记得我们之前是怎么连接mysql数据库的么?
<?php header("content-type:text/html;charset=utf-8");//字符编码设置 $servername = "localhost"; $username = "root"; $password = "root"; $dbname = "web"; // 创建连接 $con =mysqli_connect($servername, $username, $password, $dbname); // 检测连接 $sql = "select * from users"; $result = mysqli_query($con,$sql); if (!$result) { printf("error: %s\n", mysqli_error($con)); exit(); } $jarr = array(); while ($rows=mysqli_fetch_array($result,mysql_assoc)){ $count=count($rows);//不能在循环语句中,由于每次删除 row数组长度都减小 for($i=0;$i<$count;$i++){ unset($rows[$i]);//删除冗余数据 } array_push($jarr,$rows); } echo $str=json_encode($jarr);//将数组进行json编码 ?
以上就是php获取数据库中数据的实例代码的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product