您好,欢迎访问一九零五行业门户网

一起学习php7连接MySQL制作简易查询程序

简易教程
假设我们制作的是分班情况查询程序,将使用php7的环境以pdo的方式连接mysql。
通过学号和姓名查询自己所在班级。
先来介绍文件结构和数据库结构:
php:
config.php 存放数据库配置信息
cx.php 查询程序
index.html 用户界面
推荐(免费):php7
结构如图
mysql:
表名:data
字段:1.sid 2.name 3.class
结构如图
准备就绪,开始吧,现在!
首先构建用户界面(index.html),两个简单的编辑框加上一个简单的按钮:
<!doctype html><html lang="cn"><head>    <meta charset="utf-8">    <title>分班查询系统</title></head><body><form action="cx.php" method="post">    <p>学号:<input type="text" name="xuehao"></p>    <p>姓名: <input type="text" name="xingming"></p>    <p><input type="submit" name="submit" value="查询"></p></form></body></html>
好嘞,接下来配置数据库信息(config.php)吧
<?php$server="localhost";//主机的ip地址$db_username="root";//数据库用户名$db_password="123456";//数据库密码$db_name = "data";
然后去编写我们的主程序(cx.php)
<?phpheader("content-type: text/html; charset=utf8");if(!isset($_post["submit"])){ exit("未检测到表单提交");}//检测是否有submit操作include ("config.php");$sid = $_post['sid'];//post获得学号表单值$name = $_post['name'];//post获得姓名表单值echo "<table style='border: solid 1px black;'>;echo <tr><th>学号</th><th>姓名</th><th>班级</th></tr>;class tablerows extends recursiveiteratoriterator{    function __construct($it)    {        parent::__construct($it, self::leaves_only);    }    function current()    {        return <td style='width:150px;border:1px solid black;'> . parent::current() . </td>;    }    function beginchildren()    {        echo <tr>;    }    function endchildren()    {        echo </tr> . \n;    }}try {    $conn = new pdo(mysql:host=$server;dbname=$db_name, $db_username, $db_password);    $conn->setattribute(pdo::attr_errmode, pdo::errmode_exception);    $stmt = $conn->prepare(select sid, name, class from data where sid=$sid and name='$name');    $stmt->execute();    // 设置结果集为关联数组    $result = $stmt->setfetchmode(pdo::fetch_assoc);    foreach (new tablerows(new recursivearrayiterator($stmt->fetchall())) as $k => $v) {        echo $v;    }} catch (pdoexception $e) {    echo error:  . $e->getmessage();}$conn = null;echo </table>;
到此程序就写完啦
来试试看吧
以上就是一起学习php7连接mysql制作简易查询程序的详细内容。
其它类似信息

推荐信息