您的位置:首页 > 编程语言 > PHP开发

PHP 查询数据库

2014-01-26 22:35 120 查看
<?PHP //连接mysql $link=mysql_connect("127.0.0.1","root","rootpic"); if($link){ //设置编码 mysql_query("set names 'utf8'"); //打开数据库 if(mysql_select_db('Class_11_Team_Seven',$link)){ //sql语句 $sql = "select * from ZGX"; //执行查询操作 $result = mysql_query($sql); $allStudent = array(); //查询到一条数据返回结果,结果类型是array while($rows = mysql_fetch_array($result)){ //获取返回的字段数 $filedCounts = mysql_num_fields($result); $stu = array(); for($i=0;$i<$filedCounts;$i++){ //获取返回的字段 $key = mysql_field_name($result,$i); //根据字段获取值 $value = $rows[$key]; $stu[$key] = $value; }
$allStudent[] = $stu; } echo(json_encode($allStudent)); } } //关闭数据库 mysql_close($link); ?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: