Let's learn php7 to connect to MySQL and create a simple query program

coldplay.xixi
Release: 2023-02-17 20:52:02
forward
2051 people have browsed it

Let's learn php7 to connect to MySQL and 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.
Let’s first introduce the file structure and database structure:
PHP:
config.php stores database configuration information
cx.php query program
index.html user interfaceLets learn php7 to connect to MySQL and create a simple query program
Recommended (free):PHP7

The structure is as shown

MySQL:
table Name: data
Field: 1.Sid 2.name 3.class
Lets learn php7 to connect to MySQL and 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>  分班查询系统
Copy after login

学号:

姓名:

Okay, let’s configure the database information (config.php)


        
Copy after login

Then write our main program (cx.php)

";echo " 学号 姓名 班级 ";class TableRows extends RecursiveIteratorIterator{ function __construct($it) { parent::__construct($it, self::LEAVES_ONLY); } function current() { return "" . parent::current() . ""; } function beginChildren() { echo ""; } function endChildren() { echo "" . "\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 "";
Copy after login

This program is finished
Let’s give it a try
Lets learn php7 to connect to MySQL and create a simple query program
Lets learn php7 to connect to MySQL and create a simple query program

The above is the detailed content of Let's learn php7 to connect to MySQL and create a simple query program. 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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!