结合PHP脚本添加和查询MySQL数据的基本教程_MySQL
MySQL Insert Into 添加数据
INSERT INTO
INSERT INTO 语法用于向数据表中添加数据记录。
语法:
INSERT INTO tb_name VALUES (value1, value2,...)
该语法表示向表中所有的字段按顺序都插入数据记录。
但更多情况下是向指定的列添加记录:
INSERT INTO tb_name (column1, column2,...) VALUES (value1, value2,...)
下面的例子向 user 表添加一条记录:
<?php $conn = @mysql_connect("localhost","root","root123"); if (!$conn){ die("连接数据库失败:" . mysql_error()); } mysql_select_db("test", $conn); mysql_query("set names 'gbk'"); //为避免中文乱码做入库编码转换 //mysql_query("set names 'utf8'"); //PHP 文件为 utf-8 格式时使用 $password = md5("123456"); //原始密码 12345 经过加密后得到加密后密码 $regdate = time(); //得到时间戳 $sql = "INSERT INTO user(username, password, email, regdate)VALUES('小王', '$password', '12345@163.com', $regdate)"; //exit($sql); //退出程序并打印 SQL 语句,用于调试 if(!mysql_query($sql,$conn)){ echo "添加数据失败:".mysql_error(); } else { echo "添加数据成功!"; } ?>
如果是表单提交的数据,那么在数据处理页面可以使用 $_POST 或 $_GET 接收表单数据而将数据写入数据表。
说明
1.为了避免数据记录因为编码问题无法写入数据表或写入乱码,所以在执行 mysql_query() 之前,进行了编码转换
2.存储密码为实际密码经过 MD5 加密,MD5 加密不可逆,如要验证密码,只需将用户输入的密码经 MD5 加密后与数据库密码比对即可
3.在 SQL 语句中,我们使用单引号''来表示文本字符属性
4.为了调试数据写入数据表中出现的异常,增加了退出程序并打印 SQL 语句的功能,在需要调试的时候可去掉语句前面的注释,使之生效而便于调试
MySQL Select from 查询数据
普通查询
SELECT FROM 语法用于从数据表中查询读取数据。
语法:
SELECT column1,column1,... FROM tb_name
如果要读取全部字段,可以使用 * 号代替字段名:
SELECT * FROM tb_name
例子:
<?php $conn = @mysql_connect("localhost","root","root123"); if (!$conn){ die("连接数据库失败:" . mysql_error()); } mysql_select_db("test", $conn); mysql_query("set character set 'gbk'"); //避免中文乱码字符转换 mysql_query("set character set 'utf8'"); // PHP 文件为 utf-8 格式时使用 $sql = "SELECT * FROM user"; $result = mysql_query($sql); //得到查询结果数据集 //循环从数据集取出数据 while( $row = mysql_fetch_array($result) ){ echo "用户名:".$row['username']."<br />"; echo "电子邮件:".$row['email']."<br />"; echo "注册日期:".date("Y-m-d", $row[regdate])."<br /><br />"; } ?>
浏览器输出:
用户名:admin
电子邮件:admin@5idev.com
注册日期:2010-08-06
用户名:小明
电子邮件:xiao@163.com
注册日期:2010-07-02
用户名:Jack
电子邮件:jack@gmail.com
注册日期:2010-07-02
用户名:小王
电子邮件:12345@163.com
注册日期:2010-11-13
说明
1.使用 mysql_query("set character set 'gbk'") 来避免读取数据的中文乱码
2.mysql_query() 得到的是数据集资源(Resource),需要用 mysql_fetch_array() 函数来取得
3.使用 while 循环来逐行取得全部数据

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



The future of PHP will be achieved by adapting to new technology trends and introducing innovative features: 1) Adapting to cloud computing, containerization and microservice architectures, supporting Docker and Kubernetes; 2) introducing JIT compilers and enumeration types to improve performance and data processing efficiency; 3) Continuously optimize performance and promote best practices.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

You can open phpMyAdmin through the following steps: 1. Log in to the website control panel; 2. Find and click the phpMyAdmin icon; 3. Enter MySQL credentials; 4. Click "Login".

MySQL and SQL are essential skills for developers. 1.MySQL is an open source relational database management system, and SQL is the standard language used to manage and operate databases. 2.MySQL supports multiple storage engines through efficient data storage and retrieval functions, and SQL completes complex data operations through simple statements. 3. Examples of usage include basic queries and advanced queries, such as filtering and sorting by condition. 4. Common errors include syntax errors and performance issues, which can be optimized by checking SQL statements and using EXPLAIN commands. 5. Performance optimization techniques include using indexes, avoiding full table scanning, optimizing JOIN operations and improving code readability.

PHP is not dying, but constantly adapting and evolving. 1) PHP has undergone multiple version iterations since 1994 to adapt to new technology trends. 2) It is currently widely used in e-commerce, content management systems and other fields. 3) PHP8 introduces JIT compiler and other functions to improve performance and modernization. 4) Use OPcache and follow PSR-12 standards to optimize performance and code quality.

Redis uses a single threaded architecture to provide high performance, simplicity, and consistency. It utilizes I/O multiplexing, event loops, non-blocking I/O, and shared memory to improve concurrency, but with limitations of concurrency limitations, single point of failure, and unsuitable for write-intensive workloads.

How to connect to MySQL using phpMyAdmin? The URL to access phpMyAdmin is usually http://localhost/phpmyadmin or http://[your server IP address]/phpmyadmin. Enter your MySQL username and password. Select the database you want to connect to. Click the "Connection" button to establish a connection.
