gongzhonghao.png
扫码关注我们
最新赞助活动温馨提示:自愿赞助服务器费用,学生和没有工作的整站资源免费下载!
头像

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

来源:http://erdangjiade.com/topic/2085.html 你好,世界。 2017-09-25 22:42浏览(7)

配置好了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
", 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获取数据库中数据的实例代码的详细内容,更多请关注二当家的素材网其它相关文章!

评论0
头像

友情提示:垃圾评论一律封号 加我微信:826096331拉你进VIP群学习群

1 2