


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 interface
Recommended (free): PHP7
The structure is as shown
MySQL:
table Name: data
Field: 1.Sid 2.name 3.class
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, let’s 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['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>" . 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
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to install the mongo extension in php7.0: 1. Create the mongodb user group and user; 2. Download the mongodb source code package and place the source code package in the "/usr/local/src/" directory; 3. Enter "src/" directory; 4. Unzip the source code package; 5. Create the mongodb file directory; 6. Copy the files to the "mongodb/" directory; 7. Create the mongodb configuration file and modify the configuration.

To resolve the plugin not showing installed issue in PHP 7.0: Check the plugin configuration and enable the plugin. Restart PHP to apply configuration changes. Check the plugin file permissions to make sure they are correct. Install missing dependencies to ensure the plugin functions properly. If all other steps fail, rebuild PHP. Other possible causes include incompatible plugin versions, loading the wrong version, or PHP configuration issues.

Speaking of HeidiSQL software, I believe many users are familiar with it, but do you know how HeidiSQL connects to MySQL? The following content brings you the specific methods of HeidiSQL connecting to MySQL. Let’s learn together. 1. Click the New button and then modify the session name. 2. Enter the user and password, keep other options unchanged by default, and click the Save button to save this session. 3. Click the Open button. 4. Enter the database management interface, select the database, and click the query button. 5. Enter the query statement and click the execute button.

In php5, we can use the fsockopen() function to detect the TCP port. This function can be used to open a network connection and perform some network communication. But in php7, the fsockopen() function may encounter some problems, such as being unable to open the port, unable to connect to the server, etc. In order to solve this problem, we can use the socket_create() function and socket_connect() function to detect the TCP port.

Common solutions for PHP server environments include ensuring that the correct PHP version is installed and that relevant files have been copied to the module directory. Disable SELinux temporarily or permanently. Check and configure PHP.ini to ensure that necessary extensions have been added and set up correctly. Start or restart the PHP-FPM service. Check the DNS settings for resolution issues.

How to install and deploy php7.0: 1. Go to the PHP official website to download the installation version corresponding to the local system; 2. Extract the downloaded zip file to the specified directory; 3. Open the command line window and go to the "E:\php7" directory Just run the "php -v" command.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Causes and solutions for errors when using PECL to install extensions in Docker environment When using Docker environment, we often encounter some headaches...
