Home>Article>Backend Development> How to create a simple query program to connect MySQL with php7
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.
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
The structure is as shown in the figure
MySQL:
Table name: data
Fields: 1.Sid 2.name 3.
Prepare Ready, get started, now!
First build the user interface (index.html), two simple edit boxes plus a simple button:Okay, then configure the database information (config. php)分班查询系统
Then write our main program (cx.php)"; echo "This program is finished Let’s try it"; 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 "";
Recommended learning:
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!