search
HomeBackend DevelopmentPHP7Learn to connect MySQL with php7 to create a simple query program

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

Simple Tutorial
Assume that we are making a class status inquiry program and will use PHP7 environment to connect in the form of PDO MySQL.
Check your class by student number and name.

Recommended (free): PHP7
Let’s first introduce the file structure and database structure:
PHP:
config.php stores database configuration information
cx.php query program
index.html user interfaceLearn to connect MySQL with php7 to create a simple query program
The structure is as shown in the figure

MySQL:
Table Name: data
Field: 1.Sid 2.name 3.class
Learn to connect MySQL with php7 to create a simple query program
The structure is as shown in the figure

Ready, let’s start, now!
First build the user interface (index.html), two simple edit boxes and a simple button:

nbsp;html>
    <meta>
    <title>分班查询系统</title>
    

学号:

    

姓名: 

    

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

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

Then write our main program (cx.php)

<?phpheader ("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>" . 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 "";

This program is finished
Let’s give it a try
Learn to connect MySQL with php7 to create a simple query program
Learn to connect MySQL with php7 to create a simple query program

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

Statement
This article is reproduced at:CSDN. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version