Table of Contents
Register/Login user
Home Backend Development PHP Tutorial PHP and MYSQL database connection tutorial sharing

PHP and MYSQL database connection tutorial sharing

Feb 22, 2018 pm 02:10 PM
mysql php database

php and mysql database

Teaching objectives:

1. Understand the MySQL database and be proficient in using mysql (phpMyadmin)

2. PHP code Realize the connection and CURD operation to the MySQL database

Teaching content:

Detailed explanation:

1. PHP connection to the database

1. Connect to the MYSQL server

Use the mysql_connect() function to create a connection with the mysql server.

2. Select the database

Use the mysql_select_db() function to select the database on the mysql database server and create a connection with the database.

3. Execute sql statement

Use the mysql_query() function to execute sql statement in the selected database.

4. Close the result set

Mysql_free_result(); After the database operation is completed, the result set needs to be closed to release system resources.

5. Close the mysql server

Use mysql_close(); every time the mysql_connect() or mysql_query() function is used, system resources will be consumed. When a small number of users access the web site, the problem is not serious, but if the number of user connections exceeds a certain number, the system performance will decrease or even crash. In order to avoid this phenomenon, after completing the database operation, you should use the mysql_close() function to close the connection with the mysql server to save system resources.

Note: The connection to the database in PHP is a non-persistent connection, and the system will automatically recycle it. Generally, there is no need to set it to close. However, if the result set returned at one time is relatively large or the website has a large number of visits, it is best to use the mysql_close() function to release it manually.

2. Connect to the database code

1. Connect to the server and select the database

/**
*@authorMissGE
*@since2012-2-16
*@descToolclass
*HandletheMySQLdatabase
*/
$conn=mysql_connect("localhost","root","");
if($conn)
{
echo"数据库连接成功";
}
else
{
Die(数据库连接失败",mysql_error());//die()函数表示向用户输出引号中的内容,程序终止执行
}
$select=mysql_select_db("school",$conn);//选择数据库
/*mysql_query("SET
character_set_connection=utf-8,character_set_results=utf-8,character_set_Client=binary",$conn);*/
Mysql_query("setnamesutf-8");//设置数据库编码格式是utf-8
if($select)
echo"选择数据库成功";
else
echo"选择数据库失败";
?>
Copy after login

2. Add a piece of data

Example: Registered user

3. Example: User login successfully

4. Modify or delete a piece of data

login.php
include("db.php");
?>
Copy after login

Register/Login user

User name:

Password:

$uName="";
$pwd="";
if($_POST["sub"])
{
$uName=$_POST["userName"];
$pwd=$_POST["pwd"];
$sql="insertinto`users`(`uName`,`uPwd`)values('".$uName."','".$pwd."')";
$query=mysql_query($sql,$conn);
if($query)
echo"注册成功";
else
echo"注册失败";
}
if($_POST["sub_1"])
{
$uName=$_POST["userName"];
$pwd=$_POST["pwd"];
echo$pwd;
$sql="select*fromuserswhereuName='".$uName."'anduPwd='".$pwd."'";
$query=mysql_query($sql,$conn);
$row=mysql_fetch_array($query);
if($row)
echo"";else
echo"登录失败";
}
?>
Db.php
$host="localhost";
$root="root";
$rPwd="";$conn=mysql_connect($host,$root,$rPwd);
if(!$conn)
die("连接数据库服务器失败".mysql_error());$select=mysql_select_db("school",$conn);mysql_query("setnamesgb2312");
if(!$select)
echo"选择数据库失败";
?>
Index_ok.php
echo"相信您的技术,您已登陆成功";echo"返回";?>
Copy after login

Related recommendations:

The difference and usage of php database connection mysql and mysqli

Detailed explanation of the operation of using PHP to realize mysql database connection

Detailed analysis of PHP implementation of database connection pool

The above is the detailed content of PHP and MYSQL database connection tutorial sharing. For more information, please follow other related articles on the PHP Chinese website!

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

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

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

Oracle's Role in the Business World Oracle's Role in the Business World Apr 23, 2025 am 12:01 AM

Oracle is not only a database company, but also a leader in cloud computing and ERP systems. 1. Oracle provides comprehensive solutions from database to cloud services and ERP systems. 2. OracleCloud challenges AWS and Azure, providing IaaS, PaaS and SaaS services. 3. Oracle's ERP systems such as E-BusinessSuite and FusionApplications help enterprises optimize operations.

Laravel framework installation method Laravel framework installation method Apr 18, 2025 pm 12:54 PM

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

MySQL and phpMyAdmin: Core Features and Functions MySQL and phpMyAdmin: Core Features and Functions Apr 22, 2025 am 12:12 AM

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

The Continued Use of PHP: Reasons for Its Endurance The Continued Use of PHP: Reasons for Its Endurance Apr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

MySQL vs. Other Programming Languages: A Comparison MySQL vs. Other Programming Languages: A Comparison Apr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

How to safely store JavaScript objects containing functions and regular expressions to a database and restore? How to safely store JavaScript objects containing functions and regular expressions to a database and restore? Apr 19, 2025 pm 11:09 PM

Safely handle functions and regular expressions in JSON In front-end development, JavaScript is often required...

The Compatibility of IIS and PHP: A Deep Dive The Compatibility of IIS and PHP: A Deep Dive Apr 22, 2025 am 12:01 AM

IIS and PHP are compatible and are implemented through FastCGI. 1.IIS forwards the .php file request to the FastCGI module through the configuration file. 2. The FastCGI module starts the PHP process to process requests to improve performance and stability. 3. In actual applications, you need to pay attention to configuration details, error debugging and performance optimization.

See all articles