


Summary of basic skills for PHP+MariaDB database operations memo
This article mainly introduces the basic skills of PHP MariaDB database operation. It summarizes and analyzes PHP MariaDB database connection, judgment, and related operation implementation skills and precautions based on PHP MariaDB such as user login, management, and deletion based on PHP MariaDB. It is necessary to Friends can refer to
The examples in this article summarize the basic skills of PHP MariaDB database operation. I share it with you for your reference. The details are as follows:
PHP MySQL is a relatively common combination. Since I subjectively don’t like Oracle very much, and after MySQL was acquired by Oracle, some changes have occurred in my bones, so I changed it. MariaDB, a brother who still adheres to MySQL's original open source belief. They are essentially the core of MySQL, so all the following database operation codes can be used directly in MySQL.
After setting up the basic environment of PHP Apache and installing the MySQL database at night, I wrote the simplest database connection code, and the result was the following classic error: Fatal error: Class 'mysqli' not found
<?php $db = new mysqli('localhost', 'root', 'admin', 'test'); if (mysqli_connect_errno()) { echo '<p>' . 'Connect DB error'; exit; } ?>
In fact, this problem is relatively simple. From the error returned by the PHP engine, we know that mysqli has not been loaded correctly. Most of the problems are This happened in the configuration of the php.ini file. The default semicolon in front of the configuration item "extension=php_mysqli.dll" was not removed. I didn't make this mistake. There is also the file php_mysqli.dll in the ext directory in the PHP installation path. So where did the problem occur? The problem should still occur in the wrong place in the php.ini file. After some reading, I found that "extension_dir = "ext"" has not been modified. I didn't think much about it at the time, thinking that the PHP engine could automatically find this relative path. But then I thought about it, the PHP engine is loaded by Apache, and Apache does not know this relative relationship. Or honestly change this place to an absolute path, and it's OK. In fact, you can write this piece of code before this code to see if the mysqli component has been loaded. This method is suitable for pre-loading judgment of other components.
if (extension_loaded('mysqli')) { echo 'yes'; } else { echo 'no'; }
The following uses a user registration and system login to record the most basic operation method of PHP MySQL.
1. Create database, tables and users.
DROP DATABASE IF EXISTS `test`; CREATE DATABASE `test` USE `test`; DROP TABLE IF EXISTS `tbl_user`; CREATE TABLE `tbl_user` ( `username` varchar(32) NOT NULL default '', `password` varchar(32) NOT NULL default '', PRIMARY KEY (`username`) ) ENGINE=InnoDB DEFAULT CHARSET=gb2312;
2. Create registration and login html pages, which are register.html and login.html respectively. As shown in the figure below:
3. Registration and login code:
register_do.php
<?php $username = $_POST['username']; $password = $_POST['password']; $db = new mysqli('localhost', 'root', 'admin', 'test'); if (mysqli_connect_errno()) { echo '<p>' . 'Connect DB error'; exit; } $query = "select * from tbl_user where username = '" . $username . "'"; echo '<p>' . $query; $result = $db->query($query); if ($result) { echo '<p>' . 'The user '. $username .' exist'; echo '<p>' . '<a href="register.html" rel="external nofollow" rel="external nofollow" >Back to register</a>'; } else { $query = "insert into tbl_user values ('". $username ."', '". $password ."')"; echo '<p>' . $query; $result = $db->query($query); if ($result) { echo '<p>' . '<a href="register.html" rel="external nofollow" rel="external nofollow" >Register successful</a>'; } } ?>
login_do.php
<?php $username = $_POST['username']; $password = $_POST['password']; $db = new mysqli('localhost', 'root', 'admin', 'test'); if (mysqli_connect_errno()) { echo '<p>' . 'Connect DB error'; exit; } $query = "select * from tbl_user where username = '" . $username . "' and password = '" . $password . "'"; echo '<p>' . $query; $result = $db->query($query); if ($result->num_rows) { echo '<p>' . '<a href="login.html" rel="external nofollow" rel="external nofollow" >Login successful</a>'; } else { echo '<p>' . '<a href="login.html" rel="external nofollow" rel="external nofollow" >Login failed</a>'; } ?>
userlist.php
<?php $db = new mysqli('localhost', 'root', 'admin', 'test'); if (mysqli_connect_errno()) { echo '<p>' . 'Connect DB error'; exit; } echo '<p>' . 'All user as follows:'; $query = "select * from tbl_user order by username"; if ($result = $db->query($query)) { while ($row = $result->fetch_assoc()) { echo '<p>' . 'Username : ' . $row['username'] . ' <a href="userdelete.php?username=' . $row['username'] . '" rel="external nofollow" >delete</a>'; } } ?>
4. The final display effect of the page is as shown below:
5. The code to delete the user:
userdelete.php
<?php $username = $_GET['username']; $db = new mysqli('localhost', 'root', 'admin', 'test'); if (mysqli_connect_errno()) { echo '<p>' . 'Connect DB error'; exit; } $query = "delete from tbl_user where username = '" . $username . "'"; echo $query; if ($result = $db->query($query)) { echo '<p>' . 'Delete user ' . $username . ' successful'; } else { echo '<p>' . 'Delete user ' . $username . ' failed'; } echo '<p>' . '<a href="userlist.php" rel="external nofollow" >Back to user list</a>'; ?>
Prepare preprocessing
1. The book adding page is as shown below (bookadd.html):
2. The table creation script is as follows:
DROP DATABASE IF EXISTS `test`; CREATE DATABASE IF NOT EXISTS `test`; USE `test`; DROP TABLE IF EXISTS `tbl_book`; CREATE TABLE IF NOT EXISTS `tbl_book` ( `isbn` varchar(32) NOT NULL, `title` varchar(32) NOT NULL, `author` varchar(32) NOT NULL, `price` float NOT NULL, PRIMARY KEY (`isbn`) ) ENGINE=InnoDB DEFAULT CHARSET=utf-8;
3. The added logic processing code is as follows (bookadd_do.php ): What needs special attention here is the sentence "$db->query("set names utf-8")
", which means that when writing data to the database, utf-8 encoding and decoding is used. Displays the encoding and decoding settings for database table operations to prevent Chinese garbled characters. I will record an article specifically on this technical point later.
<?php $isbn = $_POST['isbn']; $title = $_POST['title']; $author = $_POST['author']; $price = $_POST['price']; $db = new mysqli('localhost', 'root', 'admin', 'test'); if (mysqli_connect_errno()) { echo '<p>' . 'Connect DB error'; exit; } $db->query("set names utf-8"); //特别注意这句话 $stmt = $db->stmt_init(); $stmt->prepare("insert into tbl_book values (?,?,?,?)"); $stmt->bind_param("sssd", $isbn, $title, $author, $price); $stmt->execute(); echo '<p>' . 'Affect rows is ' . $stmt->affected_rows; echo '<p>' . '<a href="booklist.php" rel="external nofollow" >Go to book list page</a>'; ?>
4. The logic code for displaying book information is as follows. Also pay attention to the sentence "$db->query("set names utf- 8")
":
<?php $db = new mysqli('localhost', 'root', 'admin', 'test'); if (mysqli_connect_errno()) { echo '<p>' . 'Connect DB error'; exit; } $db->query("set names utf-8"); //特别注意这句话 $stmt = $db->stmt_init(); $stmt->prepare("select * from tbl_book"); $stmt->bind_result($isbn, $title, $author, $price); $stmt->execute(); while($stmt->fetch()) { echo 'ISBN : ' . $isbn . '<p>'; echo 'Title : ' . $title . '<p>'; echo 'Author : ' . $author . '<p>'; echo 'Price : ' . $price . '<p>'; echo '<p>' . '-----------------------------' . '<p>'; } ?>
5. The displayed page is as shown below:
PHP pdo-based database operation class [can support mysql, sqlserver and oracle]
The above is the detailed content of Summary of basic skills for PHP+MariaDB database operations memo. 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

AI Hentai Generator
Generate AI Hentai for free.

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

Go language is an efficient, concise and easy-to-learn programming language. It is favored by developers because of its advantages in concurrent programming and network programming. In actual development, database operations are an indispensable part. This article will introduce how to use Go language to implement database addition, deletion, modification and query operations. In Go language, we usually use third-party libraries to operate databases, such as commonly used sql packages, gorm, etc. Here we take the sql package as an example to introduce how to implement the addition, deletion, modification and query operations of the database. Assume we are using a MySQL database.

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

Hibernate polymorphic mapping can map inherited classes to the database and provides the following mapping types: joined-subclass: Create a separate table for the subclass, including all columns of the parent class. table-per-class: Create a separate table for subclasses, containing only subclass-specific columns. union-subclass: similar to joined-subclass, but the parent class table unions all subclass columns.

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

HTML cannot read the database directly, but it can be achieved through JavaScript and AJAX. The steps include establishing a database connection, sending a query, processing the response, and updating the page. This article provides a practical example of using JavaScript, AJAX and PHP to read data from a MySQL database, showing how to dynamically display query results in an HTML page. This example uses XMLHttpRequest to establish a database connection, send a query and process the response, thereby filling data into page elements and realizing the function of HTML reading the database.

Analysis of the basic principles of the MySQL database management system MySQL is a commonly used relational database management system that uses structured query language (SQL) for data storage and management. This article will introduce the basic principles of the MySQL database management system, including database creation, data table design, data addition, deletion, modification, and other operations, and provide specific code examples. 1. Database Creation In MySQL, you first need to create a database instance to store data. The following code can create a file named "my

PHP is a back-end programming language widely used in website development. It has powerful database operation functions and is often used to interact with databases such as MySQL. However, due to the complexity of Chinese character encoding, problems often arise when dealing with Chinese garbled characters in the database. This article will introduce the skills and practices of PHP in handling Chinese garbled characters in databases, including common causes of garbled characters, solutions and specific code examples. Common reasons for garbled characters are incorrect database character set settings: the correct character set needs to be selected when creating the database, such as utf8 or u

How to integrate GoWebSocket with a database: Set up a database connection: Use the database/sql package to connect to the database. Store WebSocket messages to the database: Use the INSERT statement to insert the message into the database. Retrieve WebSocket messages from the database: Use the SELECT statement to retrieve messages from the database.
