Home > Backend Development > PHP7 > body text

How to create a simple query program to connect MySQL with php7

醉折花枝作酒筹
Release: 2023-02-17 22:38:02
forward
2431 people have browsed it

This article will introduce to you how to connect php7 to MySQL to create a simple query program. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

How to create a simple query program to connect MySQL with php7

Simple Tutorial

Assume that we are making a class status inquiry program and connect the environment using PHP7 in the form of PDO MySQL.

Check your class by student number and name.

Let’s first introduce the file structure and database structure:

PHP:

config.php stores database configuration information

cx. php query program

index.html User interface

How to create a simple query program to connect MySQL with php7

The structure is as shown in the figure

MySQL:

Table name: data

Fields: 1.Sid 2.name 3.

How to create a simple query program to connect MySQL with php7

##The structure is as shown in the figure

Prepare Ready, get started, now!

First build the user interface (index.html), two simple edit boxes plus a simple button:

<!DOCTYPE html>
<html>
<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>
Copy after login

Okay, then configure the database information (config. php)

<?php
$server="localhost";//主机的IP地址
$db_username="root";//数据库用户名
$db_password="123456";//数据库密码
$db_name = "data";
Copy after login

Then write our main program (cx.php)

<?php
header("Content-Type: text/html; charset=utf8");
if(!isset($_POST["submit"]))
{
    exit("未检测到表单提交");
}//检测是否有submit操作
include ("config.php");
$Sid = $_POST[&#39;Sid&#39;];//post获得学号表单值
$name = $_POST[&#39;name&#39;];//post获得姓名表单值
echo "<table style=&#39;border: solid 1px black;&#39;>";
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=&#39;width:150px;border:1px solid black;&#39;>" . 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=&#39;$name&#39;");
    $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>";
Copy after login

This program is finished

Let’s try it

How to create a simple query program to connect MySQL with php7

How to create a simple query program to connect MySQL with php7

Recommended learning:

php video tutorial

The above is the detailed content of How to create a simple query program to connect MySQL with php7. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template