php语言查询mysql数据库内容的方法:首先php页面在进行浏览时需要有php语言执行的环境;然后建立php类文件【mysql.php】进行具体的操作;接着建立页面文件【index.php】进行接收数据;最后访问路径即可。
【相关学习推荐:mysql教程】
php语言查询mysql数据库内容的方法:
1.php页面在进行浏览时需要有php语言执行的环境,本人用的是wampserver软件,只要将项目复制到wampserver_php\wamp\www\该路径下就可以执行php语言。
2.建立php类文件(mysql.php)进行具体的操作
<?php/*设置内容类型和编码样式*/header("content-type:text/html;charset=utf-8");/*对数据库操作*/class dbmysqli{ private $conn = null; public $message = ""; /*设置错误接受机制*/ function message($mes,$flag=true){ if($flag){ $this->message .="<div style='color:green;font-size:12px;'>".$mes."</div>"; }else{ $this->message .="<div style='color:green;font-size:12px;'>".$mes."</div>"; } } /*连接数据库服务器,设置连接数据库编码*/ function __construct($host,$user,$pwd,$dbname,$charset){ //连接数据库服务器选择数据库 $this->conn = new mysqli($host,$user,$pwd,$dbname); if($this->conn === false){ $this->message("连接数据库失败",false); return false; }else{ $this->message("连接数据库成功,选择数据库"); } //设置连接编码 if($this->conn->set_charset($charset)){ $this->message("设置编码成功"); }else{ $this->message("设置编码失败",false); } } /*查询数据库数据*/ public function moredata($sql){ $sql = trim($sql); /*检查sql语言是否正确*/ $result = preg_match('/^select/i',$sql); if($result){ //执行sql语句 $rs = $this->conn->query($sql); if($rs === false){ $this->message("执行'".$sql."'失败,失败原因:".$this->conn->error,false); return false; }else{ $this->message("执行'".$sql."'成功"); $rs = $rs->fetch_all(mysql_assoc); $rs->free(); return $rs; } }else{ $this->message("执行'".$sql."'失败",false); return false; } }}/*链接数据库地址、用户名,密码,数据库名,编码方式*/$db = new dbmysqli('localhost','root','cly8','user','utf-8');
3.建立页面文件(index.php)进行接收数据
<?php header("content-type:text/html;charset=utf-8"); error_reporting(e_all); //引入一个数据库操作类 include_once 'mysql.php'; //查询数据 $rs = $db->moredata("select * from student");?><html> <head> <meta charset="utf-8" /> <title>css3实现多彩动态漩涡线条特效动画</title> </head> <style> table{ font-family: verdana,arial,sans-serif; font-size:11px; color:#333333; border-width: 1px; border-color: #666666; border-collapse: collapse; } table th { border-width: 1px; padding: 8px; border-style: solid; border-color: #666666; background-color: #dedede; } table td { border-width: 1px; padding: 8px; border-style: solid; border-color: #666666; background-color: #ffffff; } </style><body> <table> <tr> <th>编号</th> <th>姓名</th> <th>密码</th> </tr> <?php foreach($rs as $val) {?> <tr> <td><?php echo $val['sid'];?></td> <td><?php echo $val['sname'];?></td> <td><?php echo $val['password'];?></td> </tr> <?php }?> </table></body></html>
4.最后访问路径http://localhost/文件夹名/index.php
想了解更多相关学习,敬请关注php培训栏目!
以上就是php语言如何查询mysql数据库内容的详细内容。