Table of Contents
PHP操作数据库:
Home php教程 php手册 php数据库操作命令精华大全

php数据库操作命令精华大全

Jun 06, 2016 pm 07:43 PM
php information Order Encyclopedia operate data database Essence structure

1、表结构//列信息2、表数据//行信息3、表索引//把列中的行加到索引中(一般情况下一个表一定要把id这一列的所有数据都加到主键索引中) 2、[dos下]关闭mysql:net stop mysql 开启mysql:net start mysql 登陆mysql:mysql -uroot -p123 --tee=c:\mysql.log 查

1、表结构//列信息2、表数据//行信息3、表索引//把列中的行加到索引中(一般情况下一个表一定要把id这一列的所有数据都加到主键索引中)

2、[dos下]关闭mysql:net stop mysql
开启mysql:net start mysql
登陆mysql:mysql -uroot -p123 --tee=c:\mysql.log
查看数据库命令:show database;
进入test数据库:use test
查看数据库表:show tables;
创建一个表:create table user(id int,name varchar(30),pass varchar(30));
查看表结构或表字段:desc user
查看表数据:select*from user;
查看表中的所有索引:show index from t2;
在表中插入数据:insert into user(id,name,pass) values(1,"hello","123");
查看表中的id:select*from user where id=1;
删除表中的id:delete from user where id=1;
修改表中的值:update user set name='hello' where id=1;
退出myaql:exit;
创建一个数据库:create database txet;
删除数据库:drop database;
修改表名:rename table user to user1;
删除表:drop table user1;

 

表字段的类型:
1、数值:int//int(3)与长度无关,不够3位时前面补0,默认不显示
float
2、字符串:char(n) 255字节 占用n个字节
varchar(n)最高65535字节 存多少占多少字节
text 65535字节
longtext 42亿字节
3、日期:date time datetime year

 

数据库的基础篇字段属性:
1、unsigned 无符号,全是正数
2、zenrofill 零填充,int(3),不够3位补0
3、auto_increment 自增
4、null 这一列值允许为null
5、not null 这一列值不允许为null
6、default 不允许为null给默认值

 

用\s查看四种字符集:
1、server characterset: utf8 服务器字符集
2、Db characterset: utf8 数据库字符集
3、client characterset: utf8 客户端字符集
4、Conn. characterset: utf8 客户端连接字符集
查看数据库字符集命令:sohw create database text;
查看表字符集命令:show create table user;
PHP中设置mysql客户端和连接字符集:$sql="set names utf8";

 

表字段索引:
1、主键索引
2、普通索引
检查sql语句:desc select * from user where id=3\G//加\G把表颠倒一下
rows 1 表示找到一个id=3的人检索一行找到
查看表中的所有索引:show index from user;

 

后期维护普通索引:
1、添加普通索引:alter table t2 add index in_name(name);
2、删除普通索引:alter table t2 drop index in_name;

 

后期维护数据库字段:
1、添加字段
alter table t1 add age int;
2、修改字段
alter table t1 modify age int not null default 20;
3、删除字段
alter table t1 drop age;
4、修改字段名
alter table t1 change name username varchar(30);

 

结构化查询语言sql包含四个部分:
1、DDL //数据定义语言,reate,drop,alter
2、DML //数据操作语言,insert,update,delete
3、DQL //数据查询语言,select
4、DCL //数据控制语句,grant,commit,rollback

 

增-insert:insert into t1(username) values('g');
改-update:update t1 set username='f' where id=6;
一次更改多个值:update t1 set id=10, username='bb' where id =7;
删-delete:
delete from t1 where id=6;
delete from t1 where id in(1,2,5);
delete from t1 where id=1 or id=3 or id=5;
delete from t1 where id>=3 and iddelete from t1 where id between 3 and 5;
查-select:

1、选择特定的字段:select id,name from user where id=3;
2、给字段取别名-as:select pass as p,id from user where id=3;
select pass p,id from user where id=3;
3、取掉列中的重复值:select distinct name form user;
4、使用where条件进行查询:select * from user where id>=3 and id5、查询空值null:select *from user where pass is null;
select *from user where pass is not null;
6、搜索like关键字:select * form user wher name like '%3%';
select * form user wher name like '%3%' or name like '%1%';
select * form user wher name regexp '.*3.*';
select * form user wher name regexp '(.*3.*)|(.*5.*)';
7、使用order by对查询结果排序:
升序 select * from user ordeer by id asc;注:升序可以不写asc
降序 select * from user ordeer by id desc;
8、使用limit限定输出个数:
select * from user order by id desc limit 0,3;
select * from user order by id desc limit 3;
9、concat函数-字符串连接符:select concat("a","-","c");
10、rand函数-随机排序:selec 8 from user order by rans() limit 3;
11、count统计:select count(*) from user;/ /http://www.pprar.com
select count(id) from user;
select count(id) from user where name='user1';//统计user1出现的次数
12、sum求和:select sum(id) from user where name='user1';//符合要求的id 之和
13、avg平均数: select avg(id) from user;
14、max最大值: select max(id) from user;
15、min最小值: select min(id) from user;
16、分组聚合:select name,count(id) tot from mess group by name order by tot desc;//tot是起了个别名//group by必须写在order by的前面
select name,count(id) tot from mess group by name having tot>=5;//group by必须写在having的前面//分组后加条件必须用having,而非where
17、在id字段后加uid字段:alter table post add uid int after id;
18、多表查询:
(1)普通查询-多表
(2)嵌套查询-多表
(3)左连接查询-多表
普通查询:
select user.name,count(post.id) from user,post where user.id=post.uid group by post.uid;
左连接查询:
select user.name,post.title,post.content from user left join post on user.id=post.uid;//显示全部用户发送或者没有发送的名字加内容
普通查询:
//得到发帖子的人-普通查询mysql> select distinct user.name from user,post where user.id=post.uid;
嵌套查询:
//得到发帖子的人-嵌套查询mysql> select name from user where id in(select uid from post);

PHP操作数据库:


1、通过PHP连接上mysql数据库
2、选择数据库
3、通过PHP进行insert操作
4、通过PHP进行delete操作
5、通过PHP进行update操作
6、通过PHP进行select操作
通过PHP连接mysql数据库:mysql_connect("localhost","root","123");
选择数据库:mysql_select_db("test");
设置客户端和连接字符集:mysql_query("set names utf8");
通过PHP进行insert操作://从表单接收数据$uername="user1"; $passwoed="123";
//$sql="insert into t1(username,password) values('$username','$password')";
//执行这条mysql语句 var_dump(mysql_query($sql));
//释放连接资源 mysql_close($conn);
//通过PHP进行update操作$sql="update t1 set username='user2',pssword='111' where id=8";
//通过PHP进行delete操作:$sql="delete form t1 where id=8";
从结果集中取数据:
mysql_fetch_assoc//关联数组
mysql_fetch_row//索引数组
mysql_fetch_array//混合数组
mysql_fetch_object//对象
//通过PHP进行select操作$sql="select form t1";
从结果集中取全部数据:while($row=mysql_fech_assoc($result)){echo "

"; print_r($row) echo "
Copy after login

";}
mysql_insert_id//取得上一步INSERT操作产生的ID
mysql_num_fields//得到insert,update,delete操作影响的行数musql_num_rows//得到select操作影响的行数
//得到表总行数:$sql="select count(*) form t1";
$rst=mysql_query($sql);
$tot=mysql_fetch_row($rst);

 

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 Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1248
24
PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP: Handling Databases and Server-Side Logic PHP: Handling Databases and Server-Side Logic Apr 15, 2025 am 12:15 AM

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

See all articles